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
- Vectors
- Strings and &str
- HashMaps
- Iterating Collections