Rust
Install
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env
Create a new project using
$ cargo new hello_world --bin # for a binary
$ cargo new hello_world # for a library
Exercises
- Rust exercises, resolution of the rustlings exercises
Reference
Uploading to crates.io
Use
cargo publish
Static constants
“Lazy” static constants can be defined using the lazy_static
macro from the lazy_static crate.
The crate can be added to the dependencies with
[dependencies]
lazy_static = "1.4.0"
and by adding to the Rust source code:
#[macro_use]
extern crate lazy_static;
Static “global” constants can then be added via:
lazy_static! {
static ref HASHMAP: HashMap<u32, &'static str> = {
let mut m = HashMap::new();
m.insert(0, "foo");
m.insert(1, "bar");
m.insert(2, "baz");
m
};
}
List folders recursively
Using the glob
crate:
use glob::glob;
fn main() {
for entry in glob("./**/*.md").expect("Failed to read glob pattern") {
match entry {
Ok(path) => println!("{:?}", path.display()),
Err(e) => println!("{:?}", e),
}
}
}
Duration between two times
Using Rust’s standard SystemTime
1, a duration between the present moment and a specific time can be calculated using:
let duration = SystemTime::now()
.duration_since(another_system_time)
.ok()
.unwrap();