Common Collections Flashcards

1
Q

What do vectors allow for?

A

Storing more than one value in a single data structure that puts all the values next to each other in memory.

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

What is the syntax to create a new empty vector?

A

let v: Vec = Vec::new();

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

What does let v = vec![1, 2, 3]; do

A

Uses the vec macro to create a new Vec that holds the values 1, 2, and 3, and assign it to the variable v.

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

What is the method used to add values to a vector?

A

push()

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

What are the two ways to read values from a vector?

A

By using & and [], which gives a reference, or by using the get method with the index passed as an argument, which gives an Option.

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

How can you iterate over a vector v?

A
for i in &mut v {
    // Do something.
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we store multiple types in a vector?

A

Create an enum with whatever types needed, and a vector that stores the enum type.

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

How are strings implemented in Rust?

A

As a collection of bytes, plus some methods to provide useful functionality when those bytes are interpreted as text.

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

What is the one string type in the Rust core language?

A

The string slice str that is usually seen in its borrowed form &str

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

Where is Rusts String type provided?

A

The standard library.

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

What is the encoding for both String and string slices?

A

UTF-8

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

What is the syntax to create a new String in Rust?

A

let mut s = String::new();

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

What does the to_string() do on a string literal?

A
Creates a String from a string literal.
let s = "initial contents".to_string();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the String::from function do?

A
Creates a String for a string literal.
let s = String::from("initial contents");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the method to push to a string slice?

A

push_str().

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

What does the push method do to a String?

A

Takes a single character as a param and adds it to the string.

17
Q

What are two ways to concatenate strings in Rust?

A

+ and format!

18
Q

What is a String in Rust?

A

A wrapper over a Vec.

19
Q

Why is referencing into a String by index not allowed in Rust?

A

Because it would return the byte value corresponding to the index which might not equate to a correct unicode scalar value. Also because indexing operations are supposed to take O(1) and Rust cannot guarantee that.

20
Q

What does the chars() do when used with a string?

A

Iterates over the string returning each Unicode scalar value.

21
Q

What does the bytes() do when used with a string

A

Iterates over the string and returns each byte.

22
Q

What is the use statement for HashMap?

A

use std::collections::HashMap;

23
Q

What is the syntax to create a new HashMap?

A

let mut scores = HashMap::new();