diff --git a/Cargo.toml b/Cargo.toml index 8ae4220f..bf9c6bea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/book.toml b/book.toml index f3593255..db5ce4ac 100644 --- a/book.toml +++ b/book.toml @@ -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" diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 0f34eceb..5c9f30ce 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) --- diff --git a/src/lifetimes.md b/src/lifetimes.md new file mode 100644 index 00000000..96e223e4 --- /dev/null +++ b/src/lifetimes.md @@ -0,0 +1,3 @@ +# Lifetimes + +{{%segment outline}} diff --git a/src/slices-and-lifetimes/Cargo.toml b/src/lifetimes/Cargo.toml similarity index 82% rename from src/slices-and-lifetimes/Cargo.toml rename to src/lifetimes/Cargo.toml index 62fac4b7..323eea3e 100644 --- a/src/slices-and-lifetimes/Cargo.toml +++ b/src/lifetimes/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "slices-and-lifetimes" +name = "lifetimes" version = "0.1.0" edition = "2021" publish = false diff --git a/src/slices-and-lifetimes/exercise.md b/src/lifetimes/exercise.md similarity index 100% rename from src/slices-and-lifetimes/exercise.md rename to src/lifetimes/exercise.md diff --git a/src/slices-and-lifetimes/exercise.rs b/src/lifetimes/exercise.rs similarity index 100% rename from src/slices-and-lifetimes/exercise.rs rename to src/lifetimes/exercise.rs diff --git a/src/slices-and-lifetimes/lifetime-annotations.md b/src/lifetimes/lifetime-annotations.md similarity index 100% rename from src/slices-and-lifetimes/lifetime-annotations.md rename to src/lifetimes/lifetime-annotations.md diff --git a/src/slices-and-lifetimes/lifetime-elision.md b/src/lifetimes/lifetime-elision.md similarity index 100% rename from src/slices-and-lifetimes/lifetime-elision.md rename to src/lifetimes/lifetime-elision.md diff --git a/src/slices-and-lifetimes/solution.md b/src/lifetimes/solution.md similarity index 100% rename from src/slices-and-lifetimes/solution.md rename to src/lifetimes/solution.md diff --git a/src/slices-and-lifetimes/struct-lifetimes.md b/src/lifetimes/struct-lifetimes.md similarity index 100% rename from src/slices-and-lifetimes/struct-lifetimes.md rename to src/lifetimes/struct-lifetimes.md diff --git a/src/slices-and-lifetimes/slices.md b/src/references/slices.md similarity index 100% rename from src/slices-and-lifetimes/slices.md rename to src/references/slices.md diff --git a/src/slices-and-lifetimes/str.md b/src/references/strings.md similarity index 73% rename from src/slices-and-lifetimes/str.md rename to src/references/strings.md index ba731945..9cfb9d35 100644 --- a/src/slices-and-lifetimes/str.md +++ b/src/references/strings.md @@ -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. -
- `&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. -- Rust’s `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`, 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: + + + ```rust,editable + fn main() { + println!(r#"link"#); + println!("link"); + } + ``` +
diff --git a/src/slices-and-lifetimes.md b/src/slices-and-lifetimes.md deleted file mode 100644 index 03c8206a..00000000 --- a/src/slices-and-lifetimes.md +++ /dev/null @@ -1,3 +0,0 @@ -# Slices and Lifetimes - -{{%segment outline}} diff --git a/src/types-and-values/strings.md b/src/types-and-values/strings.md deleted file mode 100644 index 47b0d13a..00000000 --- a/src/types-and-values/strings.md +++ /dev/null @@ -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]); -} -``` - -
- -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: - - - ```rust,editable - fn main() { - println!(r#"link"#); - println!("link"); - } - ``` - -- Using `{:?}` is a convenient way to print array/vector/struct of values for - debugging purposes, and it's commonly used in code. - -