2023-11-29 10:39:24 -05:00
|
|
|
---
|
|
|
|
|
minutes: 10
|
|
|
|
|
---
|
2022-12-21 16:36:30 +01:00
|
|
|
|
2023-11-29 10:39:24 -05:00
|
|
|
<!-- NOTES:
|
|
|
|
|
Including `&str` as a way of representing a slice of valid utf-8
|
|
|
|
|
-->
|
2023-12-31 00:15:07 +01:00
|
|
|
|
2024-03-14 13:21:15 -07:00
|
|
|
# Strings
|
2023-11-29 10:39:24 -05:00
|
|
|
|
2024-03-14 13:21:15 -07:00
|
|
|
We can now understand the two string types in Rust:
|
|
|
|
|
|
|
|
|
|
- `&str` is a slice of UTF-8 encoded bytes, similar to `&[u8]`.
|
2024-05-06 19:33:46 +03:00
|
|
|
- `String` is an owned buffer of UTF-8 encoded bytes, similar to `Vec<T>`.
|
2022-12-21 16:36:30 +01:00
|
|
|
|
2024-04-09 19:30:58 +02:00
|
|
|
<!-- Avoid using fixed integers when slicing since this breaks
|
|
|
|
|
translations. Using the length of s1 and s2 is safe. -->
|
|
|
|
|
|
2022-12-21 16:36:30 +01:00
|
|
|
```rust,editable
|
|
|
|
|
fn main() {
|
2023-01-24 08:52:55 +00:00
|
|
|
let s1: &str = "World";
|
2022-12-21 16:36:30 +01:00
|
|
|
println!("s1: {s1}");
|
|
|
|
|
|
|
|
|
|
let mut s2: String = String::from("Hello ");
|
|
|
|
|
println!("s2: {s2}");
|
2025-02-06 09:52:29 -08:00
|
|
|
|
2022-12-21 16:36:30 +01:00
|
|
|
s2.push_str(s1);
|
|
|
|
|
println!("s2: {s2}");
|
2023-11-29 10:39:24 -05:00
|
|
|
|
2024-04-09 19:30:58 +02:00
|
|
|
let s3: &str = &s2[s2.len() - s1.len()..];
|
2023-01-24 08:52:55 +00:00
|
|
|
println!("s3: {s3}");
|
2022-12-21 16:36:30 +01:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-13 13:14:19 +00:00
|
|
|
<details>
|
|
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- `&str` introduces a string slice, which is an immutable reference to UTF-8
|
2024-03-14 13:21:15 -07:00
|
|
|
encoded string data stored in a block of memory. String literals (`"Hello"`),
|
2023-12-31 00:15:07 +01:00
|
|
|
are stored in the program’s binary.
|
2023-01-13 13:14:19 +00:00
|
|
|
|
2024-03-14 13:21:15 -07:00
|
|
|
- Rust's `String` type is a wrapper around a vector of bytes. As with a
|
2023-12-31 00:15:07 +01:00
|
|
|
`Vec<T>`, it is owned.
|
2023-11-29 10:39:24 -05:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- As with many other types `String::from()` creates a string from a string
|
|
|
|
|
literal; `String::new()` creates a new empty string, to which string data can
|
|
|
|
|
be added using the `push()` and `push_str()` methods.
|
2023-01-13 13:14:19 +00:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- The `format!()` macro is a convenient way to generate an owned string from
|
|
|
|
|
dynamic values. It accepts the same format specification as `println!()`.
|
2023-11-29 10:39:24 -05:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- You can borrow `&str` slices from `String` via `&` and optionally range
|
|
|
|
|
selection. If you select a byte range that is not aligned to character
|
|
|
|
|
boundaries, the expression will panic. The `chars` iterator iterates over
|
2023-11-29 10:39:24 -05:00
|
|
|
characters and is preferred over trying to get character boundaries right.
|
|
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- For C++ programmers: think of `&str` as `std::string_view` from C++, but the
|
|
|
|
|
one that always points to a valid string in memory. Rust `String` is a rough
|
|
|
|
|
equivalent of `std::string` from C++ (main difference: it can only contain
|
|
|
|
|
UTF-8 encoded bytes and will never use a small-string optimization).
|
2023-11-29 10:39:24 -05:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Byte strings literals allow you to create a `&[u8]` value directly:
|
2023-11-29 10:39:24 -05:00
|
|
|
|
|
|
|
|
<!-- mdbook-xgettext: skip -->
|
|
|
|
|
```rust,editable
|
|
|
|
|
fn main() {
|
|
|
|
|
println!("{:?}", b"abc");
|
|
|
|
|
println!("{:?}", &[97, 98, 99]);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-14 13:21:15 -07:00
|
|
|
- Raw strings allow you to create a `&str` value with escapes disabled:
|
|
|
|
|
`r"\n" == "\\n"`. You can embed double-quotes by using an equal amount of `#`
|
|
|
|
|
on either side of the quotes:
|
|
|
|
|
|
|
|
|
|
<!-- mdbook-xgettext: skip -->
|
|
|
|
|
```rust,editable
|
|
|
|
|
fn main() {
|
|
|
|
|
println!(r#"<a href="link.html">link</a>"#);
|
|
|
|
|
println!("<a href=\"link.html\">link</a>");
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-13 13:14:19 +00:00
|
|
|
</details>
|