The following Rust code doesn't compile.
extern create byteorder;
use byetorder::{LittleEndian, ReadBytesExt};
fn main() {
let buf: [u8; 12] = [
0x00, 0x42, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0xc3, 0x00, 0x00, 0x30,
];
let id = &buf[0..1].read_u16::<LittleEndian>(); }
The message from the compiler:
error[E0599]: no method named `read_u16` found for type `[u8]` in the current scope
--> src/main.rs:18:25
|
18 | let id = &buf[0..1].read_u16::<LittleEndian>();
| ^^^^^^^^
|
= note: the method `read_u16` exists but the following trait bounds were not satisfied:
`[u8] : byteorder::ReadBytesExt`
There are very similar questions on Stack Overflow, which I have reviewed, but mine is subtly different from those because I'm trying to read a u16
from a slice. In practice, I'm not sure why my example is substantively different, I'm sure I'm missing something obvious.
Specifically, it's not clear to me how what I've got is meaningfully different from what's in the accepted answer here:
How can I convert a buffer of a slice of bytes (&[u8]) to an integer?
Don't I also have &[u8]
when I say &buf[0..1]
?