String and memory

Rust

examples/string-and-memory/src/main.rs

macro_rules! ptr {
    ($var: expr) => {
        println!("p: {:p} ptr: {:<15?} len: {:2}, capacity: {:2} '{}'", &$var, $var.as_ptr(), $var.len(), $var.capacity(), $var)        
    };
}

fn main() {
    // Create an empty string
    let mut text = String::new();
    ptr!(text);
    text.push('a');
    ptr!(text);

    text.push('b');
    ptr!(text);

    text.push_str("cdefgh");
    ptr!(text);

    let other = String::from("qqrq");
    ptr!(other);

    text.push('i');
    ptr!(text);

    text.push_str("12345678901234567890");
    ptr!(text);

}


Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo