Rust
Resources
Organisation
- Cargo.lock
- Cargo.toml
- src
- lib.rs (normal code, unit tests, and run function)
- main.rs (calls run function)
- tests
- integration_test.rs (for run function from lib.rs)
Python extensions
-
Make Rust modules for Python bottlenecks
Arrays
Multi-threading
-
rayon
-
par_bridge().try_for_each
enables error from one of threads to be propagated back e.g., for columns
-
par_azip
is similar for arrays
Error handling
-
anyhow
-
thiserror
-
Pattern match range of Rust errors to Python ones, so these are returned to the Python user
Cloud
Lint
Profile
Benchmark
Logging
JSON
async
Docs
-
rustdoc
-
#![warn(missing_docs)]
-
all public stuff
-
create simple examples
-
#![doc = include_str!("../README.md")]
-
cargo doc --no-deps --open
-
good example:
bed_reader - Rust (docs.rs)
Database
Builder pattern for keyword arguments
Plotting
Concatenating strings
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = format!("{s1}-{s2}-{s3}");
Using an Enum to Store Multiple Types
use std::collections::HashMap;
use std::hash::Hash;
#[derive(Debug, Eq, PartialEq, Hash)]
enum Keys {
Int(i32),
Text(String),
}
#[derive(Debug)]
#[allow(dead_code)]
enum SpreadsheetCell {
Int(i32),
Float(f64),
Text(String),
}
fn main() {
let mut scores = HashMap::new();
scores.insert(Keys::Text(String::from("Blue")), 10);
scores.insert(Keys::Text(String::from("Yellow")), 50);
scores.insert(Keys::Int(42), 30);
println!("{:#?}", scores);
let row = vec![
SpreadsheetCell::Int(3),
SpreadsheetCell::Text(String::from("blue")),
SpreadsheetCell::Float(10.12),
];
println!("{:#?}", row);
}