1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-24 08:32:57 +02:00
comprehensive-rust/src/unsafe/calling-unsafe-functions.md
2023-01-27 17:17:37 +00:00

535 B

Calling Unsafe Functions

A function or method can be marked unsafe if it has extra preconditions you must uphold to avoid undefined behaviour:

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));
    }
}