use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet, VecDeque, BinaryHeap, LinkedList};
fn vector_examples() {
let mut v = Vec::new();
v.push(1);
v.push(2);
v.push(3);
let v2 = vec![1, 2, 3, 4, 5];
let third: &i32 = &v2[2];
println!("The third element is {}", third);
match v2.get(2) {
Some(third) => println!("The third element is {}", third),
None => println!("There is no third element."),
}
for i in &mut v {
*i += 50;
}
for i in &v {
println!("{}", i);
}
}
fn hashmap_examples() {
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
let team_name = String::from("Blue");
let score = scores.get(&team_name).copied().unwrap_or(0);
println!("Blue team score: {}", score);
for (key, value) in &scores {
println!("{}: {}", key, value);
}
scores.entry(String::from("Yellow")).or_insert(100);
scores.entry(String::from("Green")).or_insert(30);
let text = "hello world wonderful world";
let mut word_counts = HashMap::new();
for word in text.split_whitespace() {
let count = word_counts.entry(word).or_insert(0);
*count += 1;
}
println!("{:?}", word_counts);
}
fn hashset_examples() {
let mut books = HashSet::new();
books.insert("Alice in Wonderland");
books.insert("The Wonderful Wizard of Oz");
books.insert("The Hobbit");
if !books.insert("The Hobbit") {
println!("We already have The Hobbit");
}
println!("We have {} books", books.len());
let mut more_books = HashSet::new();
more_books.insert("The Hobbit");
more_books.insert("The Silmarillion");
for book in books.intersection(&more_books) {
println!("Both collections have: {}", book);
}
for book in books.union(&more_books) {
println!("Either collection has: {}", book);
}
for book in books.difference(&more_books) {
println!("Only in books: {}", book);
}
}
fn vecdeque_examples() {
let mut deque = VecDeque::new();
deque.push_back(1);
deque.push_back(2);
deque.push_front(0);
println!("{:?}", deque);
if let Some(front) = deque.pop_front() {
println!("Popped from front: {}", front);
}
if let Some(back) = deque.pop_back() {
println!("Popped from back: {}", back);
}
for (i, &value) in deque.iter().enumerate() {
println!("{}: {}", i, value);
}
}
fn binary_heap_examples() {
let mut heap = BinaryHeap::new();
heap.push(1);
heap.push(5);
heap.push(2);
heap.push(4);
heap.push(3);
println!("{:?}", heap.peek());
while let Some(value) = heap.pop() {
println!("Popped: {}", value);
}
}
fn btreemap_examples() {
let mut map = BTreeMap::new();
map.insert("c", 3);
map.insert("a", 1);
map.insert("b", 2);
for (key, value) in &map {
println!("{}: {}", key, value);
}
println!("Keys >= 'b':");
for (key, value) in map.range("b"..) {
println!("{}: {}", key, value);
}
}
fn btreeset_examples() {
let mut set = BTreeSet::new();
set.insert(3);
set.insert(1);
set.insert(4);
set.insert(1);
set.insert(5);
set.insert(9);
for value in &set {
println!("{}", value);
}
println!("First: {:?}", set.first());
println!("Last: {:?}", set.last());
}
fn linked_list_examples() {
let mut list = LinkedList::new();
list.push_back(1);
list.push_back(2);
list.push_front(0);
println!("{:?}", list);
if let Some(front) = list.pop_front() {
println!("Popped front: {}", front);
}
let mut list2 = LinkedList::new();
list2.push_back(3);
list2.push_back(4);
list.append(&mut list2);
println!("After append: {:?}", list);
}
fn custom_hash_example() {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[derive(Debug)]
struct Person {
id: u32,
name: String,
phone: u64,
}
impl PartialEq for Person {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for Person {}
impl Hash for Person {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
let mut people = HashSet::new();
people.insert(Person {
id: 1,
name: String::from("Alice"),
phone: 555_555_5555,
});
println!("People: {:?}", people);
}
fn main() {
vector_examples();
hashmap_examples();
hashset_examples();
vecdeque_examples();
binary_heap_examples();
btreemap_examples();
btreeset_examples();
linked_list_examples();
custom_hash_example();
}