How do I pin indirect dependencies of a crate?

Manual editing You can check out Iron, modify Cargo.toml to specify versions (as you have already done). Then you repeat the process, checking out url, modifying its Cargo.toml, then make sure you are using your version of url in Iron’s Cargo.toml. Rinse and repeat. Patch overrides From the Cargo docs: The [patch] section of Cargo.toml … Read more

How can I convert a hex string to a u8 slice?

You can also implement hex encoding and decoding yourself, in case you want to avoid the dependency on the hex crate: use std::{fmt::Write, num::ParseIntError}; pub fn decode_hex(s: &str) -> Result<Vec<u8>, ParseIntError> { (0..s.len()) .step_by(2) .map(|i| u8::from_str_radix(&s[i..i + 2], 16)) .collect() } pub fn encode_hex(bytes: &[u8]) -> String { let mut s = String::with_capacity(bytes.len() * 2); … Read more

How to read a struct from a file in Rust?

Here you go: use std::io::Read; use std::mem; use std::slice; #[repr(C, packed)] #[derive(Debug, Copy, Clone)] struct Configuration { item1: u8, item2: u16, item3: i32, item4: [char; 8], } const CONFIG_DATA: &[u8] = &[ 0xfd, // u8 0xb4, 0x50, // u16 0x45, 0xcd, 0x3c, 0x15, // i32 0x71, 0x3c, 0x87, 0xff, // char 0xe8, 0x5d, 0x20, 0xe7, … Read more

How do I avoid unwrap when converting a vector of Options or Results to only the successful values?

I want to ignore all Err values Since Result implements IntoIterator, you can convert your Vec into an iterator (which will be an iterator of iterators) and then flatten it: Iterator::flatten: vec.into_iter().flatten().collect() Iterator::flat_map: vec.into_iter().flat_map(|e| e).collect() These methods also work for Option, which also implements IntoIterator. You could also convert the Result into an Option and … Read more