mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-02-16 01:20:08 +02:00
Add safety comment.
This commit is contained in:
parent
47e28e87c0
commit
d07171e76d
@ -1,13 +1,15 @@
|
|||||||
# Calling Unsafe Functions
|
# Calling Unsafe Functions
|
||||||
|
|
||||||
A function or method can be marked `unsafe` if it has extra preconditions you
|
A function or method can be marked `unsafe` if it has extra preconditions you
|
||||||
must uphold:
|
must uphold to avoid undefined behaviour:
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
let emojis = "🗻∈🌏";
|
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 {
|
unsafe {
|
||||||
// Undefined behavior if indices do not lie on UTF-8 sequence boundaries.
|
|
||||||
println!("{}", emojis.get_unchecked(0..4));
|
println!("{}", emojis.get_unchecked(0..4));
|
||||||
println!("{}", emojis.get_unchecked(4..7));
|
println!("{}", emojis.get_unchecked(4..7));
|
||||||
println!("{}", emojis.get_unchecked(7..11));
|
println!("{}", emojis.get_unchecked(7..11));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user