Rust Flashcards
0.26MB. 0 & 6 images. Updated 2024-03-09.
Description
Sample (from 557 notes)
Question | In a hash map, how do we return a mutable reference to a particular key, but if the key doesn’t exist, insert it first? |
Answer | Using the or_insert(…) on Entry use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.entry(String, from("Yellow")).or_insert(50); scores.entry(String::from("Blue")).or_insert(50); println!("{:?}", scores); // {"Yellow": 50, "Blue": 10} The or_insert() method on Entry is defined to return a mutable reference to the value for the corresponding Entry key if that key exists, and if not, inserts the parameter as the new value for this key and returns a mutable reference to the new value. |
Guid | "7w~Vn?KXWl" |
Question | How do you initialize a vector with pre existing initial values? |
Answer | With the vec! macro: let v = vec![1, 2, 3]; |
Guid | "Fpa@+e;/:i" |
Question | What is the difference between the String type and the string literal? |
Answer | The String type manages data allocated on the heap and as such is able to store an amount of text that is unknown to us at compile time. let mut greet = String::from("Hello"); greet.push_str(", world!"); // Appends a literal to a String println!("{}", greet); // Prints "Hello, world!" |
Guid | "YdXqJg}`dn" |
After the file is downloaded, double-click on it to open it in the desktop program.
At this time, it is not possible to add shared decks directly to your AnkiWeb account - they need to be added from the desktop then synchronized to AnkiWeb.
Reviews
on
Excellent project
on
Perfect, helps me to find things I do not know yet
on
Appreciate the time and effort to put this together