8 - Common Collections Flashcards

1
Q
  1. Define an empty vector v1 of type f64.
  2. Define vector v2 with elements 1, 2, 3.
  3. Push an element to v1.
A
let mut v1: Vec = Vec::new();
let v2 = vec![1, 2, 3];
v1.push(3.141592654);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s wrong here?

let mut v = vec![1, 2, 3, 4, 5];
let first = &v[0];
v.push(6);
println!(“The first element is: {}”, first);

A

let mut v = vec![1, 2, 3, 4, 5];
let first = &v[0]; // immutable borrow occurs here
v.push(6); // mutable borrow occurs here
println!(“The first element is: {}”, first);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given a Vec named v, add 1 to each element in it.

A

*v needs to be mutable

for x in &mut v {
*x += 1;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does UTF-8 stands for?

A

8-Bit Universal Character Set Transformation Format

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What trait implementation is needed in order to be able to use the “to_string” method?

A

The to_string method is available on any type that implements the Display trait.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Show three ways to concatenate two String variables s1 and s2.

A
s1.push_str(&s2) // result in s1
/* Or */
let s3 = s1 + &s2 // s1 is moved to s3
/* Or - copying both strings here */
let s3 = format!("{}{}", s1, s2); // doesn't take ownership
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What's wrong with the following code:
let s1 = String::from("hello");
let h = s1[0];
A

s1[0]

String cannot be indexed by {integer}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the internal representation of a String in Rust?

A

A String is a wrapper over a Vec. Each letter is encoded in UTF-8 and can be represented with 1 to 4 bytes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are three relevant ways too look at strings from Rust’s perspective?

A
  1. as bytes - u8 vector
  2. as scalar values - unicode scalar values, which are Rust’s char type
  3. as grapheme clusters - closest thing to what we would call to letters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will happen when we run this?

  1. let hello = “Здравствуйте”;
  2. let s = &hello[0..4];
  3. let s = &hello[0..1];
A
2. let s = &hello[0..4]; 
s will be a &str that contains the first 4 bytes of the string, each character in our case will take two bytes, which means that s will be "Зд"
  1. let s = &hello[0..1];
    will panic in runtime:
    thread ‘main’ panicked at ‘byte index 1 is not a char boundary; it is inside ‘З’ (bytes 0..2) of Здравствуйте
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you iterate over strings?

A

for b in s.bytes() {…} // iterate u8 values
for c in s.chars() {…} // char, scalar values

// for graphems, use the unicode-segmentation crate:
use unicode_segmentation::UnicodeSegmentation;
for g in s.graphemes(true) {...} // true for extended grapheme clusters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Where do HashMaps and Vectors store their data?

A

on the heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create a HashMap with teams as keys and initial_scores as values, where:
let teams = vec![String::from(“Blue”),
String::from(“Yellow”)];
let initial_scores = vec![10, 50];

A

use std::collections::HashMap;

let mut scores: HashMap =
teams.into_iter()
.zip(initial_score.into_iter())
.collect();

*The type annotation HashMap is needed here because it’s possible to collect into many different data structures and Rust doesn’t know which you want unless you specify

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to iterate a HashMap?

A

for (key, value) in &mymap {…}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to update the value of the key String::from(“Blue”)?

How to update only if the key exists?

A
// will insert/update
mymap.insert(String::from("Blue"), new_val); 
// won't change the value of "Blue" if it is already exist
mymap.entry(String::from("Blue")).or_insert(new_val);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Count the number each word appears in text in the following code:

let mut mymap = HasMap::new();
for word in text.split_whitespace() {
   /* write your code here */
}
A
let mut mymap = HasMap::new();
for word in text.split_whitespace() {
   let count = map.entry(word).or_insert(0);
   *count += 1
}
17
Q

What hash function is used by default by HasMap?

A

By default, HashMap uses a hashing function called SipHash that can provide resistance to Denial of Service (DoS) attacks involving hash tables