pub fn to_string<T>(value: &T) -> Result<String>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::Serialize—T’sSerializeimpl returned an error (customserde::ser::Error, non-string mapping key that cannot be coerced, …).Error::DepthLimit— the value graph exceedsSerializerConfig::max_depth(default 128). Useto_string_with_configto 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"));