Q16. Which example correctly uses std::collections::HashMap's Entry API to populate counts?
use std::collections::HashMap;
fn main() {
let mut counts = HashMap::new();
let text = "LinkedIn Learning";
for c in text.chars() {
// Complete this block
}
println!("{:?}", counts);
}
for c in text.chars() {
if let Some(count) = &mut counts.get(&c) {
counts.insert(c, *count + 1);
} else {
counts.insert(c, 1);
};
}
for c in text.chars() {
let count = counts.entry(c).or_insert(0);
*count += 1;
}
for c in text.chars() {
let count = counts.entry(c);
*count += 1;
}
for c in text.chars() {
counts.entry(c).or_insert(0).map(|x| x + 1);
}
Q17. Which fragment does not incur memory allocations while writing to a "file" (represented by a Vec)?
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut v = Vec::<u8>::new();
let a = "LinkedIn";
let b = 123;
let c = '๐ง';
// replace this line
println!("{:?}", v);
Ok(())
}
Q27. Your application requires a single copy of some data type T to be held in memory that can be accessed by multiple threads. What is the thread-safe wrapper type?
Q60. Which types are not allowed within an enum variant's body?
Q61. Which example correctly uses std::collections::HashMap's Entry API to populate counts?
use std::collections::HashMap;
fn main() {
let mut counts = HashMap::new();
let text = "LinkedIn Learning";
for c in text.chars() {
// Complete this block
}
println!("{:?}", counts);
}
for c in text.chars() {
if let Some(count) = &mut counts.get(&c) {
counts.insert(c, *count + 1);
} else {
counts.insert(c, 1);
};
}
for c in text.chars() {
let count = counts.entry(c).or_insert(0);
*count += 1;
}
for c in text.chars() {
let count = counts.entry(c);
*count += 1;
}
for c in text.chars() {
counts.entry(c).or_insert(0).map(|x| x + 1);
}
Q62. To convert a Result to an Option, which method should you use?
Q63. Which statement about this code is true?
fn main() {
let c = 'z';
let heart_eyed_cat = '๐ป';
}
Q64. What is an alternative way of writing slice that produces the same result?
...
let s = String::form("hello");
let slice = &s[0..2];
Q65. How would you select the value 2.0 from this tuple?
let pt = Point2D(-1.0, 2.0)
Q66. What is the purpose of the move keyword in Rust?