Safe function wrapping unsafe code
⚓ Rust 📅 2026-07-08 👤 surdeus 👁️ 2In chapter 20, about the basics of unsafe Rust, the book has this example:
use std::slice;
fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
let len = values.len();
let ptr = values.as_mut_ptr();
assert!(mid <= len);
unsafe {
(
slice::from_raw_parts_mut(ptr, mid),
slice::from_raw_parts_mut(ptr.add(mid), len - mid),
)
}
}
My confusion is that I would expect anything unsafe always produce unsafe functions.
The reason is that, in that example above, there could be an error such as returning overlapping mutable references.
In that case, there would be data races wouldn't it?
In that case, assuming any of these functions may have safety errors, it should also return raw pointers, and be unsafe.
7 posts - 4 participants
🏷️ Rust_feed