Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Capacity of string

  • len
  • capacity
  • macro_rules!
macro_rules! prt {
    ($var:expr) => {
        println!("{:2} {:2} {:?}", $var.len(), $var.capacity(), $var,);
    };
}

fn main() {
    let mut text = String::new();
    prt!(text);

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

    text.push_str(" black ");
    prt!(text);

    text.push_str("cat");
    prt!(text);

    text.push_str(" climebed");
    prt!(text);
}
 0  0 ""
 1  8 "A"
 8  8 "A black "
11 16 "A black cat"
20 32 "A black cat climebed"