How should you do pointer arithmetic in Rust?
Pointers have an offset
method for pointer arithmetic.
fn main() {
let items = [1usize, 2, 3, 4];
let ptr = &items[1] as *const usize;
println!("{}", unsafe { *ptr });
println!("{}", unsafe { *ptr.offset(-1) });
println!("{}", unsafe { *ptr.offset(1) });
}
Output
2
1
3
https://doc.rust-lang.org/nightly/book/first-edition/unsafe.html