Pointer arithmetic in Rust

I tried Rust today.

First, I was interested in pointer arithmetic in Rust.

In C, we can compile pointer arithmetic.

<stddef.h>
int main() {
int tmp;
int *a = NULL;
int *b = &tmp;
printf("%td\n", a - b); // difference between two pointers
printf("%d\n", a < b); // compare between pointers
return 0;
}

Rust forbids pointer arithmetic but permits pointer comparison based memory address order relation.

fn main() {
let tmp = 0i32;
let a = &tmp as *const i32;
let b = &0 as *const i32;
//println!("{}", a - b); //compile error!!
println!("{}", a < b); // compare between two pointers
}

References and slices are never used as memory address.

References are implicitly replaced by dereferenced value as needed.

fn main() {
let tmp = 0i32;
let a = &tmp;
let b = &0i32;
println!(“{}”, a — b); // return 0
println!(“{}”, a == b); // return true
}

Comparison between two slices returns result based lexicographical order of content of slice.

fn main() {
let tmp = [0i32; 5];
let a = &tmp[0 .. 1];
let b = &tmp[1 .. 2];
// println!(“{}”, a — b); // compile error!!
println!(“{}”, a == b); // return true
}