0PricingLogin
Learn Rust Coding · Lesson

Strings and &str

Owned and borrowed text.

Two String Types

Rust has two main text types:

  • String — an owned, growable string.
  • &str — a borrowed string slice, a view into text.
fn main() {
    let owned: String = String::from("hello");
    let borrowed: &str = "world";
    println!("{} {}", owned, borrowed);
}

String Literals Are &str

When you write text in double quotes, you get a &str. It is fixed and stored in the program binary.

fn main() {
    let greeting = "Hi there"; // type is &str
    println!("{}", greeting);
}

All lessons in this course

  1. Vectors
  2. Strings and &str
  3. HashMaps
  4. Iterating Collections
← Back to Learn Rust Coding