i tried to use Memcmp but returns me the error as follows:
error[E0277]: the trait bound `u64: Memcmp` is not satisfied
--> src\main.rs:176:23
|
176 | let mem_compare = Memcmp::memcmp(&rdbf[0], &wrbuf[0] );
| ^^^^^^^^^^^^^^ the trait `Memcmp` is not implemented for `u64`
|
= help: the following other types implement trait `Memcmp`:
[i16]
[i32]
[i64]
[i8]
[u16]
[u32]
[u64]
[u8]
How can i avoid this error and to compare these arrays?
Edit: didn't read your error message well enough, didn't realize you only want to compare the first element. You can generate one-element slices using a range index like this:
The memcmp crate gives you a boolean.if you need a boolean you just can use the == operator. The memcmp crate was created over 8 years ago because the comparison of slices was not properly optimized yet.
If you want to know which of the two buffers is smaller if interpreted as native endian u8 you can use the bytemuck crate
use core::cmp::Ordering;
fn rust_memcmp_64(x: &[u8], y: &[u64]) -> Ordering {
let x: &[u8] = bytemuck::cast_slice(x);
let y: &[u8] = bytemuck::cast_slice(y);
x.cmp(y)
}
This will compile to a call of memcmp or more efficient code if the lenght is known at compile time.
In C, you can (and are supposed to) refer to (a part of) an array with a pointer to its first element, but that certainly does not fly in Rust because it’s terribly unsafe. Rust has slices for that purpose.