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.

How to match trait implementors

You can’t. Traits do not support downcasting – Rust is not inheritance/subtyping-based language, and it gives you another set of abstractions. Moreover, what you want to do is unsound – traits are open (everyone can implement them for anything), so even if in your case match *f covers all possible cases, in general the compiler … Read more

How to filter a vector of custom structs?

It’s very important programming skill to learn how to create a minimal, reproducible example. Your problem can be reduced to this: struct Vocabulary; fn main() { let numbers = vec![Vocabulary]; let other_numbers: Vec<Vocabulary> = numbers.iter().collect(); } Let’s look at the error message for your case: error[E0277]: a collection of type `std::vec::Vec<Vocabulary>` cannot be built from … Read more

How can I download Rust API docs?

Rustup If you use rustup, the recommended way to install and update Rust, then the docs may already be installed; the default installation behavior has changed over time. Try running rustup doc to open them in your browser. If they aren’t already installed, you can download the docs by running rustup component add rust-docs. By … Read more

Lazy sequence generation in Rust

Rust does have generators, but they are highly experimental and not currently available in stable Rust. Works in stable Rust 1.0 and above Range handles your concrete example. You can use it with the syntactical sugar of ..: fn main() { let sum: u64 = (0..1_000_000).sum(); println!(“{}”, sum) } What if Range didn’t exist? We … Read more

What is monomorphisation with context to C++?

Monomorphization means generating specialized versions of generic functions. If I write a function that extracts the first element of any pair: fn first<A, B>(pair: (A, B)) -> A { let (a, b) = pair; return a; } and then I call this function twice: first((1, 2)); first((“a”, “b”)); The compiler will generate two versions of … Read more

How can I build multiple binaries with Cargo?

You can specify multiple binaries using [[bin]], as mentioned in the Cargo Book [[bin]] name = “daemon” path = “src/daemon/bin/main.rs” [[bin]] name = “client” path = “src/client/bin/main.rs” You can run individual binaries with the cargo run command with the –bin <bin-name> option. Tip: If you instead put these files in src/bin/daemon.rs and src/bin/client.rs, you’ll get … Read more

How can I conditionally provide a default reference without performing unnecessary computation when it isn’t used?

You don’t have to create the default vector if you don’t use it. You just have to ensure the declaration is done outside the if block. fn accept(input: &Vec<String>) { let def; let vec = if input.is_empty() { def = vec![“empty”.to_string()]; &def } else { input }; // … do something with `vec` } Note … Read more

Can’t understand Rust module system

Short answer: you don’t need the mod phone in phone.rs. Just remove that and your code will work (assuming the rest of the code is correct). Longer answer: The following code in main.rs: pub mod phone; is equivalent to: pub mod phone { // literally insert the contents of phone.rs here } so you don’t … Read more