Based on a talk I gave at the Bay Area Rust Meetup in October 2018.
find
ls
ruby! {
class Console {
def log(string: String) {
println!("LOG: {}", string);
}
}
}
/// A basic example
#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
/// Activate debug mode
#[structopt(short = "d", long = "debug")]
debug: bool,
/// Set speed
#[structopt(short = "s", long = "speed", default_value = "42")]
speed: f64,
/// Number of cars
#[structopt(short = "c", long = "nb-cars")]
nb_cars: Option<i32>,
}
fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
#[derive(Debug, Fail)]
enum ToolchainError {
#[fail(display = "invalid toolchain name: {}", name)]
InvalidToolchainName { name: String },
#[fail(display = "unknown toolchain version: {}", version)]
UnknownToolchainVersion { version: String }
}
pub fn read_toolchains(path: PathBuf) -> Result<Toolchains, Error> {
// ... can use ? on ToolchainError, io Error and other errors
}
lazy_static! {
static ref HASHMAP: HashMap<u32, &'static str> = {
let mut m = HashMap::new();
m.insert(0, "foo");
m
};
}
fn main() {
println!("`0` is \"{}\".", HASHMAP.get(&0).unwrap());
}
bitflags! {
struct Flags: u32 {
const A = 0b00000001;
const B = 0b00000010;
const C = 0b00000100;
const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
}
}
fn main() {
let e1 = Flags::A | Flags::C;
let e2 = Flags::B | Flags::C;
assert_eq!((e1 | e2), Flags::ABC); // union
assert_eq!((e1 & e2), Flags::C); // intersection
assert_eq!((e1 - e2), Flags::A); // set difference
assert_eq!(!e2, Flags::A); // set complement
}
#[macro_use] extern crate quickcheck;
quickcheck! {
fn prop(xs: Vec<u32>) -> bool {
xs == reverse(&reverse(&xs))
}
}
#[macro_use] extern crate proptest;
proptest! {
#[test]
fn parses_all_valid_dates(ref s in "[0-9]{4}-[0-9]{2}-[0-9]{2}") {
parse_date(s).unwrap();
}
}
Vector
, Matrix
, Point
, a few othersTranslation
)use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
input.par_iter() // <-- just change that!
.map(|&i| i * i)
.sum()
}
use faster::*;
let lots_of_3s = (&[-123.456f32; 128][..]).simd_iter()
.simd_map(f32s(0.0), |v| {
f32s(9.0) * v.abs().sqrt().rsqrt().ceil().sqrt() - f32s(4.0) - f32s(2.0)
})
.scalar_collect();
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
#[derive(Serialize, Deserialize, Debug)]
struct Point { x: i32, y: i32 }
fn main() {
let point = Point { x: 1, y: 2 };
let serialized = serde_json::to_string(&point).unwrap();
let deserialized: Point = serde_json::from_str(&serialized).unwrap();
println!("ser = {}, de = {:?}", serialized, deserialized);
}