Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Created August 22, 2016 10:17
Show Gist options
  • Save KodrAus/b75c7e4d2f3240b9ab0f2864ba9ca6ad to your computer and use it in GitHub Desktop.
Save KodrAus/b75c7e4d2f3240b9ab0f2864ba9ca6ad to your computer and use it in GitHub Desktop.
Rust Collections
fn main() {
//The vec! macro is sugar for inline Vec<T> building
let vec1 = vec!["a", "b", "c"];
let vec2 = vec![1, 2, 3];
//Rust has iterators, which are usually cheaper than looping
for (&x, &y) in vec1.iter().zip(vec2.iter()) {
println!("{}, {}", x, y);
}
println!("");
//You can iterate over more than just vectors
for i in 0..10 {
println!("{}", i);
}
println!("");
//You can filter and map too (could use filter_map)
//Those '|'s are a closure. Rust functions have their own types; Fn, FnOnce and FnMut
for i in (0..10)
.filter(|i| i % 2 == 0)
.map(|i| i * 2) {
println!("{}", i);
}
//Other collection types include:
// - arrays (the size is part of the type)
// - BTreeSet / BTreeMap for ordered collections
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment