Strings and memory allocation



examples/strings/strings-and-memory-reallocation/src/main.rs
macro_rules! prt {
    ($var:expr) => {
        println!(
            "{:2} {:2} {:p} {:15?} '{}'",
            $var.len(),
            $var.capacity(),
            &$var,
            $var.as_ptr(),
            $var
        );
    };
}
fn main() {
    let mut text = String::new();
    prt!(text);
    text.push('a');
    prt!(text);

    let name = String::from("foobar");
    prt!(name);

    text.push('b');
    prt!(text);
    text.push_str("123456");
    prt!(text);

    text.push('x');
    prt!(text);

    text.push_str("123456789123143274368741");
    prt!(text);
}

 0  0 0x7ffce1f9ce58             0x1 ''
 1  8 0x7ffce1f9ce58  0x655352d24b80 'a'
 6  6 0x7ffce1f9d640  0x655352d24ba0 'foobar'
 2  8 0x7ffce1f9ce58  0x655352d24b80 'ab'
 8  8 0x7ffce1f9ce58  0x655352d24b80 'ab123456'
 9 16 0x7ffce1f9ce58  0x655352d24b80 'ab123456x'
33 33 0x7ffce1f9ce58  0x655352d24bc0 'ab123456x123456789123143274368741'