1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-17 22:57:35 +02:00

Add a slide about writing unsafe functions.

This commit is contained in:
Andrew Walbran
2023-01-17 17:13:10 +00:00
parent 3cadad4e0a
commit 6e7916c29b
3 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,18 @@
# Calling Unsafe Functions
A function or method can be marked `unsafe` if it has extra preconditions you
must uphold to avoid undefined behaviour:
```rust,editable
fn main() {
let emojis = "🗻∈🌏";
// Safe because the indices are in the correct order, within the bounds of
// the string slice, and lie on UTF-8 sequence boundaries.
unsafe {
println!("{}", emojis.get_unchecked(0..4));
println!("{}", emojis.get_unchecked(4..7));
println!("{}", emojis.get_unchecked(7..11));
}
}
```