[][src]Function fixed_width::to_string

pub fn to_string<T: FixedWidth + Serialize>(record: &T) -> Result<String>

Serializes the given type that implements FixedWidth and Serialize to a String.

Example

#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate fixed_width;
use fixed_width::{Field, FixedWidth};

#[derive(Serialize)]
struct Record {
    pub name: String,
    pub room: usize,
}

impl FixedWidth for Record {
    fn fields() -> Vec<Field> {
        vec![
            Field::default().range(0..4),
            Field::default().range(4..8),
        ]
    }
}

fn main() {
    let record = Record { name: "Carl".to_string(), room: 1234 };
    let s = fixed_width::to_string(&record).unwrap();

    assert_eq!(s, "Carl1234");
}