Skip to content

Commit

Permalink
Mention string slices with basic types.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpallant committed Aug 25, 2023
1 parent 05cc19b commit 84bd123
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion training-slides/src/basic-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,24 @@ fn main() {

Note:

- Use `.get()` method on the slice to avoid panics instead of accessing via index.
- Use `.get()` method on the slice to avoid panics instead of accessing via index.
- The range syntax include the first value but excludes the last value. Use `0..=1` to include both ends.

## String Slices

* Strings Slices (`&str`) are a special kind of `&[u8]`
* They are *guaranteed* to be a valid UTF-8 encoded Unicode string
* It is *undefined behaviour* to create one that isn't valid UTF-8
* Slicing must be done on *character boundaries*

```rust []
fn main() {
let hello_world: &str = "Hello 😀";
println!("Start = {}", &hello_world[0..5]);
// println!("End = {}", &hello_world[7..]);
}
```

Note:

Use [`std::str::from_utf8`](https://doc.rust-lang.org/std/str/fn.from_utf8.html) to make an `&str` from a `&[u8]`

0 comments on commit 84bd123

Please sign in to comment.