9951 explained code solutions for 126 technologies


rustHow to convert Rust bytes to a struct?


To convert Rust bytes to a struct, you can use the from_slice method from the std::convert::TryFrom trait. This method takes a slice of bytes and attempts to convert it into a struct.

Example code

use std::convert::TryFrom;

#[derive(Debug)]
struct MyStruct {
    a: u8,
    b: u16,
    c: u32,
}

fn main() {
    let bytes = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06];
    let my_struct = MyStruct::try_from(&bytes[..]).unwrap();
    println!("{:?}", my_struct);
}

Output example

MyStruct { a: 1, b: 515, c: 12845056 }

Code explanation

  • #[derive(Debug)]: This attribute is used to enable the Debug trait for the MyStruct struct, which allows it to be printed with the println! macro.
  • MyStruct::try_from(&bytes[..]): This method call attempts to convert the slice of bytes into an instance of the MyStruct struct.
  • unwrap(): This method call is used to unwrap the Result returned by the try_from method, which will either contain the converted struct or an error.
  • println!("{:?}", my_struct): This macro call prints the MyStruct instance to the console.

Helpful links

Edit this code on GitHub