1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-25 11:02:50 +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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 46 additions and 88 deletions

View File

@ -13,12 +13,12 @@ members = [
"src/exercises/concurrency/chat-async",
"src/generics",
"src/iterators",
"src/lifetimes",
"src/memory-management",
"src/methods-and-traits",
"src/modules",
"src/pattern-matching",
"src/references",
"src/slices-and-lifetimes",
"src/smart-pointers",
"src/std-traits",
"src/std-types",

View File

@ -87,9 +87,9 @@ use-boolean-and = true
"basic-syntax/rustdoc.html" = "../std-types/docs.html"
"basic-syntax/scalar-types.html" = "../types-and-values/values.html"
"basic-syntax/scopes-shadowing.html" = "../control-flow-basics/blocks-and-scopes.html"
"basic-syntax/slices.html" = "../slices-and-lifetimes/slices.html"
"basic-syntax/slices.html" = "../references/slices.html"
"basic-syntax/static-and-const.html" = "../unsafe-rust/static-and-const.html"
"basic-syntax/string-slices.html" = "../slices-and-lifetimes/str.html"
"basic-syntax/string-slices.html" = "../references/strings.html"
"basic-syntax/type-inference.html" = "../types-and-values/inference.html"
"basic-syntax/variables.html" = "../types-and-values/variables.html"
"control-flow-basics/conditionals.html" = "if.html"
@ -170,9 +170,9 @@ use-boolean-and = true
"ownership/borrowing.html" = "../borrowing/shared.html"
"ownership/copy-clone.html" = "../memory-management/copy-types.html"
"ownership/double-free-modern-cpp.html" = "../memory-management/move.html"
"ownership/lifetimes-data-structures.html" = "../slices-and-lifetimes/struct-lifetimes.html"
"ownership/lifetimes-function-calls.html" = "../slices-and-lifetimes/lifetime-elision.html"
"ownership/lifetimes.html" = "../slices-and-lifetimes/lifetime-annotations.html"
"ownership/lifetimes-data-structures.html" = "../lifetimes/struct-lifetimes.html"
"ownership/lifetimes-function-calls.html" = "../lifetimes/lifetime-elision.html"
"ownership/lifetimes.html" = "../lifetimes/lifetime-annotations.html"
"ownership/move-semantics.html" = "../memory-management/move.html"
"ownership/moved-strings-rust.html" = "../memory-management/move.html"
"ownership/moves-function-calls.html" = "../memory-management/move.html"
@ -183,6 +183,14 @@ use-boolean-and = true
"pattern-matching/match-guards.html" = "../tuples-and-arrays/match.html"
"running-the-course/day-4.html" = "course-structure.html"
"sintaxe-básica/funções-interlude.html" = "../basic-syntax/functions-interlude.html"
"slices-and-lifetimes.html" = "lifetimes.html"
"slices-and-lifetimes/exercise.html" = "../lifetimes/exercise.html"
"slices-and-lifetimes/lifetime-annotations.html" = "../lifetimes/lifetime-annotations.html"
"slices-and-lifetimes/lifetime-elision.html" = "../lifetimes/lifetime-elision.html"
"slices-and-lifetimes/slices.html" = "../references/slices.html"
"slices-and-lifetimes/solution.html" = "../lifetimes/solution.html"
"slices-and-lifetimes/str.html" = "../references/strings.html"
"slices-and-lifetimes/struct-lifetimes.html" = "../lifetimes/struct-lifetimes.html"
"std.html" = "std-types/std.html"
"std/box-niche.html" = "../smart-pointers/box.html"
"std/box-recursive.html" = "../smart-pointers/box.html"
@ -218,6 +226,7 @@ use-boolean-and = true
"traits/trait-bounds.html" = "../generics/trait-bounds.html"
"traits/trait-objects.html" = "../smart-pointers/trait-objects.html"
"tuples-and-arrays/match.html" = "../pattern-matching/match.html"
"types-and-values/strings.html" = "../references/strings.html"
"unsafe.html" = "unsafe-rust/unsafe.html"
"unsafe/calling-unsafe-functions.html" = "../unsafe-rust/unsafe-functions.html"
"unsafe/extern-functions.html" = "../unsafe-rust/unsafe-functions.html"

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>