1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-12-12 19:45:38 +02:00

Move slices and strings to references section (#1898)

This PR moves the slides for slices and strings into the day 1 section
on references. This seems like the more natural place to introduce
slices since slices are a type of reference. It then also made sense to
me to follow that with the introduction of `&str` and `String`, since
students now have the context to understand what a "string slice" is. I
also removed the strings slide from the types and values section since
it didn't make sense to cover the same topic twice in the same day. I
tested this new organization in my class on Wednesday and it didn't
cause day 1 to take too long.
This commit is contained in:
Nicole L
2024-03-14 13:21:15 -07:00
committed by GitHub
parent 4b27e28e7f
commit 7cd25c0262
15 changed files with 46 additions and 88 deletions

View File

@@ -25,7 +25,6 @@
- [Variables](types-and-values/variables.md)
- [Values](types-and-values/values.md)
- [Arithmetic](types-and-values/arithmetic.md)
- [Strings](types-and-values/strings.md)
- [Type Inference](types-and-values/inference.md)
- [Exercise: Fibonacci](types-and-values/exercise.md)
- [Solution](types-and-values/solution.md)
@@ -56,6 +55,8 @@
- [References](references.md)
- [Shared References](references/shared.md)
- [Exclusive References](references/exclusive.md)
- [Slices: `&[T]`](references/slices.md)
- [Strings](references/strings.md)
- [Exercise: Geometry](references/exercise.md)
- [Solution](references/solution.md)
- [User-Defined Types](user-defined-types.md)
@@ -151,14 +152,12 @@
- [Interior Mutability](borrowing/interior-mutability.md)
- [Exercise: Health Statistics](borrowing/exercise.md)
- [Solution](borrowing/solution.md)
- [Slices and Lifetimes](slices-and-lifetimes.md)
- [Slices: `&[T]`](slices-and-lifetimes/slices.md)
- [String References](slices-and-lifetimes/str.md)
- [Lifetime Annotations](slices-and-lifetimes/lifetime-annotations.md)
- [Lifetime Elision](slices-and-lifetimes/lifetime-elision.md)
- [Struct Lifetimes](slices-and-lifetimes/struct-lifetimes.md)
- [Exercise: Protobuf Parsing](slices-and-lifetimes/exercise.md)
- [Solution](slices-and-lifetimes/solution.md)
- [Lifetimes](lifetimes.md)
- [Lifetime Annotations](lifetimes/lifetime-annotations.md)
- [Lifetime Elision](lifetimes/lifetime-elision.md)
- [Struct Lifetimes](lifetimes/struct-lifetimes.md)
- [Exercise: Protobuf Parsing](lifetimes/exercise.md)
- [Solution](lifetimes/solution.md)
---

3
src/lifetimes.md Normal file
View File

@@ -0,0 +1,3 @@
# Lifetimes
{{%segment outline}}

View File

@@ -1,5 +1,5 @@
[package]
name = "slices-and-lifetimes"
name = "lifetimes"
version = "0.1.0"
edition = "2021"
publish = false

View File

@@ -6,10 +6,12 @@ minutes: 10
Including `&str` as a way of representing a slice of valid utf-8
-->
# String References
# Strings
We can now understand the two string types in Rust: `&str` is almost like
`&[char]`, but with its data stored in a variable-length encoding (UTF-8).
We can now understand the two string types in Rust:
- `&str` is a slice of UTF-8 encoded bytes, similar to `&[u8]`.
- `String` is an owned, heap-allocated buffer of UTF-8 bytes.
```rust,editable
fn main() {
@@ -26,18 +28,13 @@ fn main() {
}
```
Rust terminology:
- `&str` an immutable reference to a string slice.
- `String` a mutable string buffer.
<details>
- `&str` introduces a string slice, which is an immutable reference to UTF-8
encoded string data stored in a block of memory. String literals (`Hello`),
encoded string data stored in a block of memory. String literals (`"Hello"`),
are stored in the program’s binary.
- Rusts `String` type is a wrapper around a vector of bytes. As with a
- Rust's `String` type is a wrapper around a vector of bytes. As with a
`Vec<T>`, it is owned.
- As with many other types `String::from()` creates a string from a string
@@ -67,4 +64,16 @@ Rust terminology:
}
```
- 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>");
}
```
</details>

View File

@@ -1,3 +0,0 @@
# Slices and Lifetimes
{{%segment outline}}

View File

@@ -1,59 +0,0 @@
---
minutes: 5
---
# Strings
Rust has two types to represent strings, both of which will be covered in more
depth later. Both _always_ store UTF-8 encoded strings.
- `String` - a modifiable, owned string.
- `&str` - a read-only string. String literals have this type.
```rust,editable
fn main() {
let greeting: &str = "Greetings";
let planet: &str = "🪐";
let mut sentence = String::new();
sentence.push_str(greeting);
sentence.push_str(", ");
sentence.push_str(planet);
println!("final sentence: {}", sentence);
println!("{:?}", &sentence[0..5]);
//println!("{:?}", &sentence[12..13]);
}
```
<details>
This slide introduces strings. Everything here will be covered in more depth
later, but this is enough for subsequent slides and exercises to use strings.
- Invalid UTF-8 in a string is UB, and this not allowed in safe Rust.
- `String` is a user-defined type with a constructor (`::new()`) and methods
like `s.push_str(..)`.
- The `&` in `&str` indicates that this is a reference. We will cover references
later, so for now just think of `&str` as a unit meaning "a read-only string".
- The commented-out line is indexing into the string by byte position. `12..13`
does not end on a character boundary, so the program panics. Adjust it to a
range that does, based on the error message.
- 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>");
}
```
- Using `{:?}` is a convenient way to print array/vector/struct of values for
debugging purposes, and it's commonly used in code.
</details>