You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-17 06:37:34 +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:
@ -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>
|
Reference in New Issue
Block a user