The main differences between the String and str types in Rust are:
Ownership: String is an owned type, while str is a borrowed type. This means that when you create a String, you own the memory that it uses. When you create a str, you are borrowing a reference to a string that is owned by another object.
Mutableness: String is a mutable type, while str is an immutable type. This means that you can change the value of a String, but you cannot change the value of a str.
Size: String is a growable type, while str is a fixed-size type. This means that String can grow in size as you add more characters to it, while str cannot.
Use cases: String is typically used when you need to own a string, such as when you are storing a string in a variable or passing a string to a function. str is typically used when you need to borrow a reference to a string, such as when you are passing a string to a function that does not need to modify the string.
Here is a table that summarizes the differences between String and str:
Feature
String
str
Ownership
Owned
Borrowed
Mutableness
Mutable
Immutable
Size
Growable
Fixed-size
Use cases
Owning a string
Borrowing a reference to a string
Here is an example of how to use String and str:
Rust
fn main() {
// Create a String.
let mut string = String::from("This is a string.");
// Change the value of the String.
string.push_str(" And this is another part of the string.");
// Print the value of the String.
println!("{}", string);
// Create a str.
let str = "This is a string literal.";
// Print the value of the str.
println!("{}", str);
}
In this example, the String variable is created and initialized with the value "This is a string.". The push_str() method is then used to add the value " And this is another part of the string." to the String variable. The println!() macro is then used to print the value of the String variable.
The str variable is then created with the value "This is a string literal.". The println!() macro is then used to print the value of the str variable.
As you can see, the String variable is mutable, while the str variable is immutable. The String variable can also grow in size, while the str variable cannot.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The main differences between the String and str types in Rust are:
Here is a table that summarizes the differences between String and str:
Here is an example of how to use String and str:
Rust
In this example, the String variable is created and initialized with the value "This is a string.". The push_str() method is then used to add the value " And this is another part of the string." to the String variable. The println!() macro is then used to print the value of the String variable.
The str variable is then created with the value "This is a string literal.". The println!() macro is then used to print the value of the str variable.
As you can see, the String variable is mutable, while the str variable is immutable. The String variable can also grow in size, while the str variable cannot.