8 - Common Collections Flashcards
- Define an empty vector v1 of type f64.
- Define vector v2 with elements 1, 2, 3.
- Push an element to v1.
let mut v1: Vec = Vec::new(); let v2 = vec![1, 2, 3]; v1.push(3.141592654);
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);
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);
Given a Vec named v, add 1 to each element in it.
*v needs to be mutable
for x in &mut v {
*x += 1;
}
What does UTF-8 stands for?
8-Bit Universal Character Set Transformation Format
What trait implementation is needed in order to be able to use the “to_string” method?
The to_string method is available on any type that implements the Display trait.
Show three ways to concatenate two String variables s1 and s2.
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
What's wrong with the following code: let s1 = String::from("hello"); let h = s1[0];
s1[0]
String
cannot be indexed by {integer}
What is the internal representation of a String in Rust?
A String is a wrapper over a Vec. Each letter is encoded in UTF-8 and can be represented with 1 to 4 bytes.
What are three relevant ways too look at strings from Rust’s perspective?
- as bytes - u8 vector
- as scalar values - unicode scalar values, which are Rust’s char type
- as grapheme clusters - closest thing to what we would call to letters
What will happen when we run this?
- let hello = “Здравствуйте”;
- let s = &hello[0..4];
- let s = &hello[0..1];
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 "Зд"
- 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 can you iterate over strings?
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
Where do HashMaps and Vectors store their data?
on the heap
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];
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 to iterate a HashMap?
for (key, value) in &mymap {…}
How to update the value of the key String::from(“Blue”)?
How to update only if the key exists?
// 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);