This prints `Some("L\'Allemagne Moderne")` because there was already a key for `1`, so we didn't insert `Le Petit Prince`.
`HashMap` has a very interesting method called `.entry()` that you definitely want to try out. With it you can try to make an entry and use another method like `.or_insert()` to insert the value if there is no key. The interesting part is that it also gives a mutable reference so you can change it if you want. First is an example where we just insert `true` every time we insert a book title into the `HashMap`.
Let's pretend that we have a library and want to keep track of our books.
[Here is the page for Entry](https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html). Here is a simple version of its code. `K` means key and `V` means variable.
```rust
// 🚧
// 🚧
use std::collections::hash_map::*;
enum Entry<K,V> {
@ -4299,7 +4297,7 @@ use std::collections::BinaryHeap;
fn main() {
let mut jobs = BinaryHeap::new();
// Add jobs to do throughout the day
// Add jobs to do throughout the day
jobs.push((100, "Write back to email from the CEO"));
jobs.push((80, "Finish the report today"));
jobs.push((5, "Watch some YouTube"));
@ -4387,7 +4385,7 @@ fn check_remaining(input: &VecDeque<(&str, bool)>) { // Each item is a (&str, bo
fn done(input: &mut VecDeque<(&str, bool)>) {
let mut task_done = input.pop_back().unwrap(); // pop off the back
task_done.1 = true; // now it's done - mark as true
task_done.1 = true; // now it's done - mark as true
input.push_front(task_done); // put it at the front now
}
@ -5655,12 +5653,12 @@ fn main() {
let mut my_vec_iter = my_vec.iter(); // This is an Iterator type now, but we haven't called it yet
assert_eq!(my_vec_iter.next(), Some(&'a')); // Call the first item with .next()
assert_eq!(my_vec_iter.next(), Some(&'b')); // Call the next
assert_eq!(my_vec_iter.next(), Some(&'a'));// Call the first item with .next()
assert_eq!(my_vec_iter.next(), Some(&'b'));// Call the next
assert_eq!(my_vec_iter.next(), Some(&'거')); // Again
assert_eq!(my_vec_iter.next(), Some(&'柳')); // Again
assert_eq!(my_vec_iter.next(), None); // Nothing is left: just None
assert_eq!(my_vec_iter.next(), None); // You can keep calling .next() but it will always be None
assert_eq!(my_vec_iter.next(), None); // You can keep calling .next() but it will always be None
let mut hit_points_vec = vec![]; // Put our hit points data in here
hit_points_vec.push(*billy); // Push *billy?
hit_points_vec.push(*brandy); // Push *brandy?
hit_points_vec.push(*billy); // Push *billy?
hit_points_vec.push(*brandy); // Push *brandy?
println!("{:?}", hit_points_vec);
}
```
@ -10662,8 +10657,6 @@ mod print_things {
pub fn prints_one_thing<T:Display>(input: T) {
println!("{}", input)
}
}
fn main() {
@ -10671,7 +10664,6 @@ fn main() {
let my_billy = Billy::new(3);
my_billy.print_billy();
}
```
@ -11805,15 +11797,15 @@ You can get a char from `u8` using the `From` trait, but for a `u32` you use `Tr
```rust
use std::convert::TryFrom; // You need to brig TryFrom in to use it
use rand::prelude::*; // We will use random numbers too
use rand::prelude::*; // We will use random numbers too
fn main() {
let some_character = char::from(99); // This one is easy - no need for TryFrom
println!("{}", some_character);
let mut random_generator = rand::thread_rng();
// This will try 40,000 times to make a char from a u32.
// The range is 0 (std::u32::MIN) to u32's highest number (std::u32::MAX). If it doesn't work, we will give it '-'.
// This will try 40,000 times to make a char from a u32.
// The range is 0 (std::u32::MIN) to u32's highest number (std::u32::MAX). If it doesn't work, we will give it '-'.
for _ in 0..40_000 {
let bigger_character = char::try_from(random_generator.gen_range(std::u32::MIN, std::u32::MAX)).unwrap_or('-');
print!("{}", bigger_character)
@ -11946,8 +11938,8 @@ fn main() {
let vanuatu = Country::new("Vanuatu", 307_815, 820_000_000);
let micronesia = Country::new("Micronesia", 104_468, 367_000_000);
// We could have given Country a &str instead of a String for the name. But we would have to write lifetimes everywhere
// and that would be too much for a small example. Better to just clone them when we call println!.
// We could have given Country a &str instead of a String for the name. But we would have to write lifetimes everywhere
// and that would be too much for a small example. Better to just clone them when we call println!.
println!("{}", nauru.clone());
println!("{}", nauru.clone() + vanuatu.clone());
println!("{}", nauru + vanuatu + micronesia);
@ -12340,7 +12332,7 @@ impl fmt::Display for Ring { // Display to show who has it and who wants it
}
fn main() {
let mut one_ring = Ring::new("Frodo", "Gollum", "Sauron"); //
let mut one_ring = Ring::new("Frodo", "Gollum", "Sauron");
println!("{}", one_ring);
mem::swap(&mut one_ring.owner, &mut one_ring.former_owner); // Gollum got the ring back for a second
println!("{}", one_ring);
@ -12554,7 +12546,7 @@ The standard library has a prelude too, which is why you don't have to write thi
What if you don't want the prelude for some reason? Just add the attribute `#![no_implicit_prelude]`. Let's give it a try and watch the compiler complain: