Created
December 5, 2021 20:25
-
-
Save JosephTLyons/2cd29995c72d44b2e4b7cd41c3239a07 to your computer and use it in GitHub Desktop.
Examples of some Rust Crates that mimic some common Python data structures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use counter::Counter; | |
use defaultmap::DefaultHashMap; | |
fn counter_example() { | |
let counter = "Hello, world!".chars().collect::<Counter<_>>(); | |
for (item, count) in counter.most_common() { | |
println!("{}: {}", item, count); | |
} | |
} | |
fn default_map_example() { | |
let mut default_map: DefaultHashMap<&str, Vec<u32>> = DefaultHashMap::new(vec![]); | |
for animal in ["dog", "cat", "mouse"] { | |
for number in 0..10 { | |
default_map[animal].push(number); | |
} | |
} | |
for key in default_map.keys() { | |
let value_string = default_map[key] | |
.iter() | |
.map(|a| a.to_string()) | |
.collect::<Vec<String>>() | |
.join(", "); | |
println!("{}: [{}]", key, value_string) | |
} | |
} | |
fn main() { | |
counter_example(); | |
default_map_example(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment