Shallow compare for slices

You can use std::ptr::eq on slices:

use std::ptr;

let a = [1, 2, 3, 4];
let b = [1, 2, 3];

// True if the slices have the same address and length:
assert!(ptr::eq(&a[..3], &a[..3]));

// False if the slices have different lengths...
assert!(!ptr::eq(&a[..3], &a[..2]));

// ...or different addresses:
assert!(!ptr::eq(&a[..3], &b[..3]));

(Playground)

9 Likes