mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-02 07:37:24 +02:00
29 lines
941 B
Markdown
29 lines
941 B
Markdown
# 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!("emoji: {}", emojis.get_unchecked(0..4));
|
|
println!("emoji: {}", emojis.get_unchecked(4..7));
|
|
println!("emoji: {}", emojis.get_unchecked(7..11));
|
|
}
|
|
|
|
println!("char count: {}", count_chars(unsafe { emojis.get_unchecked(0..7) }));
|
|
|
|
// Not upholding the UTF-8 encoding requirement breaks memory safety!
|
|
// println!("emoji: {}", unsafe { emojis.get_unchecked(0..3) });
|
|
// println!("char count: {}", count_chars(unsafe { emojis.get_unchecked(0..3) }));
|
|
}
|
|
|
|
fn count_chars(s: &str) -> usize {
|
|
s.chars().map(|_| 1).sum()
|
|
}
|
|
```
|