How can I get the current time in milliseconds?

Since Rust 1.8, you do not need to use a crate. Instead, you can use SystemTime and UNIX_EPOCH: use std::time::{SystemTime, UNIX_EPOCH}; fn main() { let start = SystemTime::now(); let since_the_epoch = start .duration_since(UNIX_EPOCH) .expect(“Time went backwards”); println!(“{:?}”, since_the_epoch); } If you need exactly milliseconds, you can convert the Duration. Rust 1.33 let in_ms = since_the_epoch.as_millis(); … Read more

Split a module across several files

Rust’s module system is actually incredibly flexible and will let you expose whatever kind of structure you want while hiding how your code is structured in files. I think the key here is to make use of pub use, which will allow you to re-export identifiers from other modules. There is precedent for this in … Read more

How do I create a HashMap literal?

There isn’t a map literal syntax in Rust. I don’t know the exact reason, but I expect that the fact that there are multiple data structures that act maplike (such as both BTreeMap and HashMap) would make it hard to pick one. Rust 1.56 Many collections now offer conversions from an array argument using From … Read more

How do you pass a Rust function as a parameter?

Sure you can: fn fun_test(value: i32, f: &dyn Fn(i32) -> i32) -> i32 { println!(“{}”, f(value)); value } fn times2(value: i32) -> i32 { 2 * value } fn main() { fun_test(5, &times2); } As this is Rust, you have to take into account the ownership and lifetime of the closure. TL;DR; Basically there are … Read more

How can I update a value in a mutable HashMap?

Indexing immutably and indexing mutably are provided by two different traits: Index and IndexMut, respectively. Currently, HashMap does not implement IndexMut, while Vec does. The commit that removed HashMap‘s IndexMut implementation states: This commit removes the IndexMut impls on HashMap and BTreeMap, in order to future-proof the API against the eventual inclusion of an IndexSet … Read more

How do I invoke a system command and capture its output?

std::process::Command allows for that. There are multiple ways to spawn a child process and execute an arbitrary command on the machine: spawn — runs the program and returns a value with details output — runs the program and returns the output status — runs the program and returns the exit code One simple example from … Read more

How do I convert from an integer to a string?

Use to_string() (running example here): let x: u32 = 10; let s: String = x.to_string(); println!(“{}”, s); You’re right; to_str() was renamed to to_string() before Rust 1.0 was released for consistency because an allocated string is now called String. If you need to pass a string slice somewhere, you need to obtain a &str reference … Read more

How do I fix the Rust error “linker ‘cc’ not found” for Debian on Windows 10?

The Linux Rust installer doesn’t check for a compiler toolchain, but seems to assume that you’ve already got a C linker installed! The best solution is to install the tried-and-true gcc toolchain. sudo apt install build-essential If you need to target another architecture, install the appropriate toolchain and target the compilation as follows: rustc –target=my_target_architecture … Read more