Skip to main content

to_string

Function to_string 

Source
pub fn to_string<T>(value: &T) -> Result<String>
where T: ?Sized + Serialize,
Expand description

Serialize a Rust value to a YAML String.

Uses SerializerConfig::default: 2-space indent, no --- / ... markers, block style for collections, auto-style scalars, block scalars enabled.

§Errors

Returns Error when:

  • Error::SerializeT’s Serialize impl returned an error (custom serde::ser::Error, non-string mapping key that cannot be coerced, …).
  • Error::DepthLimit — the value graph exceeds SerializerConfig::max_depth (default 128). Use to_string_with_config to raise the cap when serialising a deliberately deep structure.

to_string itself does not perform any I/O and never returns Error::Io.

§Examples

use serde::Serialize;

#[derive(Serialize)]
struct Config {
    name: String,
    port: u16,
}

let config = Config {
    name: "myapp".to_string(),
    port: 8080,
};

let yaml = noyalib::to_string(&config).unwrap();
assert!(yaml.contains("name: myapp"));
assert!(yaml.contains("port: 8080"));