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 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

How to pass rustc flags to cargo?

You can pass flags through Cargo by several different means: cargo rustc, which only affects your crate and not its dependencies. The RUSTFLAGS environment variable, which affects dependencies as well. Some flags have a proper Cargo option, e.g., -C lto and -C panic=abort can be specified in the Cargo.toml file. Add flags in .cargo/config using … Read more

How to initialize the logger for integration tests?

You can use something like this: use std::sync::Once; static INIT: Once = Once::new(); /// Setup function that is only run once, even if called multiple times. fn setup() { INIT.call_once(|| { env_logger::init().unwrap(); }); } Then simply call setup() in the beginning of each test. Originally based on this blogpost.