Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### `HashMap`
- - Stores values by key, growable and shrinkable.
- - Any type impls the `Eq` and `Hash` trais can be a key. Includs:
- - `bool` (not useful)
- - `int`,`uint`, and all variantions thereof
- - `f32` and `f64` donot have implement `Hash` as they might cause error
- - `String` and `&str`
- - Can have a `HashMap` keyed by `String` and call `.get()` with `&str`
- - Collection classes implement `Eq` and `Hash` if contained type impls.
- - Use `#[derive(PartialEq, Eq, Hash)]` for custom type
- - create a HashMap with a certain starting capacity using HashMap::with_capacity(uint), or use HashMap::new() to get a HashMap with a default initial capacity (recommended).
- #### `HashSet`
- - Used when only keys are cared.(`HashSet<T>` wraps of `HashMap<T, ()>`)
- - Comparing to `Vec`, it donot have duplicate elements.
- - Sets have 4 primary operations: (all following calls return iterator)
- - `union`: get all unique elements in both sets.
- - `difference`: get all elements only in 1st set
- - `intersection`: get all elements those are in both sets
- - `symmetric_difference`: get all elements those are not in both sets.
- ### `Rc` (Reference Counting)
- - Keeps track num of references (owner nums of value wrapped inside `Rc`)
- - Increase 1 when `Rc` is cloned, and decrease 1 when one clone is dropped
- - When `Rc` count becomes 0, both `Rc` and the value are all dropped.
- ### `Arc` (Atomically Reference Counted)
- - For shared ownership between threads.
- - via `Clone`, create a ref pointer to heap value and increase ref count
- - The variable is dropped when all ref points go out of scope
- ```rust
- use std::collections::HashMap;
- // Eq requires that you derives PartialEq on the type
- #[derive(Eq, PartialEq, Hash)]
- struct Account<'a>
- {
- username: <'a> &str,
- password: <'a> &str,
- }
- struct AccountInfo<'a>
- {
- name: <'a> &str,
- email: <'a> &str,
- }
- type Accounts<'a> = HashMap<Account<'a>, AccountInfo<'a>);
- fn try_logon<'a>(accounts: &Accounts<'a>,
- username: &'a str, password: &'a str) {
- println!("Username: {}"
- }
- fn call(number: &str) -> &str {
- match number {
- "798-1364" => "We're sorry, the call cannot be completed as dialed.
- Please hang up and try again.",
- "645-7689" => "Hello, this is Mr. Awesome's Pizza. My name is Fred.
- What can I get for you today?",
- _ => "Hi! Who is this again?"
- }
- }
- fn main() {
- let mut contacts = HashMap::new();
- contacts.insert("Daniel", "798-1364");
- contacts.insert("Ashley", "645-7689");
- contacts.insert("Katie", "435-8291");
- contacts.insert("Robert", "956-1745");
- // Takes a reference and returns Option<&V>
- match contacts.get(&"Daniel") {
- Some(&number) => println!("Calling Daniel: {}", call(number)),
- _ => println!("Don't have Daniel's number."),
- }
- // `HashMap::insert()` returns `None`
- // if the inserted value is new, `Some(value)` otherwise
- contacts.insert("Daniel", "164-6743");
- match contacts.get(&"Ashley") {
- Some(&number) => println!("Calling Ashley: {}", call(number)),
- _ => println!("Don't have Ashley's number."),
- }
- contacts.remove(&"Ashley");
- // `HashMap::iter()` returns an iterator that yields
- // (&'a key, &'a value) pairs in arbitrary order.
- for (contact, &number) in contacts.iter() {
- println!("Calling {}: {}", contact, call(number));
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement