pub fn to_string(value: &impl Serialize) -> Result<String, Error>
Expand description

Serializes value into a String.

See serializer for information about the data format.

Examples

Basic usage:

#[derive(Serialize)]
struct Labels {
    method: Method,
    path: String,
}

#[derive(Serialize)]
enum Method {
    #[serde(rename = "GET")]
    Get,
}

let labels = Labels {
    method: Method::Get,
    path: "/metrics".to_string(),
};

let serialized = to_string(&labels).unwrap();

assert_eq!(serialized, r#"method="GET",path="/metrics""#);

Optional values:

#[derive(Serialize)]
struct Error {
    severity: Option<&'static str>,
    reason: Option<&'static str>,
}

let error = Error {
    severity: Some("fatal"),
    reason: None,
};

let serialized = to_string(&error).unwrap();

assert_eq!(serialized, r#"severity="fatal",reason="""#);