How to convert `&[u8]` to `u32`?
⚓ Rust 📅 2025-11-13 👤 surdeus 👁️ 5For learning purposes, I am trying to convert &[u8] into u32.
I read the compiler's errors applied the suggestions, but could not get it to convert.
Here is one that runs fine, and the commented out fails, I can not understand why.
use num_traits::FromBytes; // 0.2.19
// fn convert<T: num_traits::FromBytes>(data:&[u8]){
// let value:T = FromBytes::from_le_bytes(data);
// println!("{value}")
// }
fn main() {
let a = &[1,0,0,0];
let value:u32 = FromBytes::from_le_bytes(a);
println!("{value}");
// let _ = convert::<u32>(a);
}
6 posts - 2 participants
🏷️ Rust_feed