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

Include git commit hash as string into Rust program

Since Rust 1.19 (cargo 0.20.0), thanks to https://github.com/rust-lang/cargo/pull/3929, you can now define a compile-time environment variable (env!(…)) for rustc and rustdoc via: println!(“cargo:rustc-env=KEY=value”); So OP’s program can be written as: // build.rs use std::process::Command; fn main() { // note: add error checking yourself. let output = Command::new(“git”).args(&[“rev-parse”, “HEAD”]).output().unwrap(); let git_hash = String::from_utf8(output.stdout).unwrap(); println!(“cargo:rustc-env=GIT_HASH={}”, git_hash); } … Read more

Is there a way to execute a teardown function after all tests have been run?

Here is an example implementation of the custom test framework solution mentioned by Masklinn: #![feature(custom_test_frameworks)] #![feature(test)] #![test_runner(custom_test_runner)] extern crate test; use test::{test_main_static, TestDescAndFn}; fn main() {} pub fn custom_test_runner(tests: &[&TestDescAndFn]) { println!(“Setup”); test_main_static(tests); println!(“Teardown”); } #[cfg(test)] mod tests { #[test] fn test1() { println!(“Test 1”) } #[test] fn test2() { println!(“Test 2”) } } This … Read more

How do I sort a map by order of insertion?

None of the standard library collections maintain insertion order. You can instead use IndexMap from the indexmap crate, which preserves insertion order as long as you don’t call remove. use indexmap::indexmap; let map = indexmap! { “Z” => 1, “T” => 2, “R” => 3, “P” => 4, “K” => 5, “W” => 6, }; … Read more

Unable to create a local function because “can’t capture dynamic environment in a fn item”

Functions in Rust don’t capture variables from the surrounding environment, period. A “local” function in Rust is really just a global function that isn’t globally visible; it can’t do anything more than any other global function. Instead, Rust has closures which are distinct from functions in that they do capture variables from their environment. That … Read more

Automatically implement traits of enclosed type for Rust newtypes (tuple structs with one field)

is there a way to do it without extracting their “inner” values every time with pattern matching, and without implementing the Add, Sub, … traits and overloading operators? No, the only way is to implement the traits manually. Rust doesn’t have an equivalent to the Haskell’s GHC extension GeneralizedNewtypeDeriving which allows deriving on wrapper types … Read more

Step by step instruction to install Rust and Cargo for mingw with Msys2?

The Using Rust on Windows page you linked to dates from before rustup replaced the installer as the default option to install Rust. Installers are still available, but you should use rustup if possible, because it makes it easy to update and to use multiple toolchains at once (e.g. stable, beta and nightly). If you … Read more

cannot return reference to temporary value

Anything starting with & in Rust is a reference to a value, rather than a value itself. A &[u8] is a reference to a value which needs to exist elsewhere. Because you create the value it’s a reference to within the function, when the function returns, the value the reference points to no longer exists. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)