1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-05 01:55:31 +02:00
Marko Zagar fe21b773e7
Day2: Afternoon - speaker note details and minor cosmetic changes. (#408)
* A few speaker notes in Day2: Afternoon and minor cosmetic changes.

* Do not test filesystem example code block.
2023-02-16 03:19:25 +00:00

969 B

String

String is the standard heap-allocated growable UTF-8 string buffer:

fn main() {
    let mut s1 = String::new();
    s1.push_str("Hello");
    println!("s1: len = {}, capacity = {}", s1.len(), s1.capacity());

    let mut s2 = String::with_capacity(s1.len() + 1);
    s2.push_str(&s1);
    s2.push('!');
    println!("s2: len = {}, capacity = {}", s2.len(), s2.capacity());

    let s3 = String::from("🇨🇭");
    println!("s3: len = {}, number of chars = {}", s3.len(),
             s3.chars().count());
}

String implements Deref<Target = str>, which means that you can call all str methods on a String.

  • len() returns the size of the String in bytes, not its length in characters.
  • chars() returns an iterator over the actual characters.