How do you disable dead code warnings at the crate level in Rust?

You can either: Add an allow attribute on a struct, module, function, etc.: #[allow(dead_code)] struct SemanticDirection; Add an allow as a crate-level attribute; notice the !: #![allow(dead_code)] Pass it to rustc: rustc -A dead_code main.rs Pass it using cargo via the RUSTFLAGS environment variable: RUSTFLAGS=”$RUSTFLAGS -A dead_code” cargo build

How do I print a vector of u8 as a string?

If you look at the String documentation, there are a few methods you could use. There’s String::from_utf8 that takes a Vec<u8>, and there’s also String::from_utf8_lossy which takes a &[u8]. Note that a Vec<T> is more-or-less an owned, resizable wrapper around a [T]. That is, if you have a Vec<u8>, you can turn it into a … Read more

Type issue with Iterator collect

The type &(&str, &str) comes from what iter() on a Vec returns: fn iter(&self) -> Iter<T> where Iter<T> implements Iterator<Item=&T>: impl<‘a, T> Iterator for Iter<‘a, T> { type Item = &’a T … } In other words, iter() on a vector returns an iterator yielding references into the vector. cloned() solves the problem because it … Read more

Setting the include path with bindgen

With the API you can use Builder::clang_arg with arbitrary arguments: let b = bindgen::builder().header(“foo.h”).clang_arg(“-I/path”); From the command line you can do the same by appending arguments after –, like: bindgen foo.h — -I/path

How to programmatically get the number of fields of a struct?

Are there any possible API like field_count() or is it only possible to get that via macros? There is no such built-in API that would allow you to get this information at runtime. Rust does not have runtime reflection (see this question for more information). But it is indeed possible via proc-macros! Note: proc-macros are … Read more

Is it possible to share a HashMap between threads without locking the entire HashMap?

I don’t see how your request is possible, at least not without some exceedingly clever lock-free data structures; what should happen if multiple threads need to insert new values that hash to the same location? In previous work, I’ve used a RwLock<HashMap<K, Mutex<V>>>. When inserting a value into the hash, you get an exclusive lock … Read more

How to pass RUST_BACKTRACE=1 when running a Rust binary installed in Debian?

Just in case someone is looking for setting environment variable from source code, here is how you do it: use std::env; fn main() { // this method needs to be inside main() method env::set_var(“RUST_BACKTRACE”, “1”); } The benefit of this approach — in contrast to manually setting the env variable from PowerShell– is that you … Read more

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