mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-08 02:26:14 +02:00
21 lines
392 B
Markdown
21 lines
392 B
Markdown
|
# `String` vs `str`
|
||
|
|
||
|
We can now understand the two string types in Rust:
|
||
|
|
||
|
```rust,editable
|
||
|
fn main() {
|
||
|
let s1: &str = "Hello";
|
||
|
println!("s1: {s1}");
|
||
|
|
||
|
let mut s2: String = String::from("Hello ");
|
||
|
println!("s2: {s2}");
|
||
|
s2.push_str(s1);
|
||
|
println!("s2: {s2}");
|
||
|
}
|
||
|
```
|
||
|
|
||
|
Rust terminology:
|
||
|
|
||
|
* `&str` an immutable reference to a string slice.
|
||
|
* `String` a mutable string buffer
|