- i64 as i32
- usize as u64
- i32 as i64
- f64 as f32
str::thread::spawn(|| {
println!("LinkedIn");
});
- a closure
- a thread
- a future
- a block
- integer
- float
- boolean
- tuple
- Traits
- Tuples
- Enums
- Structs
- cargo --version
- cargo init
- cargo build
- cargo check
Q6. The term box and related phrases such as boxing a value are often used when relating to memory layout. What does box refer to?
- It's creating a pointer on the heap that points to a value on the stack.
- It's creating a pointer on the stack that points to a value on the heap.
- It's creating a memory guard around values to prevent illegal access.
- It's an abstraction that refers to ownership. "Boxed" values are clearly labelled.
...
let s = String::form("hello");
let slice = &s[0..2];
- let slice = &s[len + 2];
- let slice = &s[len - 2];
- let slice = &s.copy(0..2);
- let slice = &s[..2];
- a match pattern that branches into True or False
- calling ok_error()
- calling panic!()
- a match pattern that may result an early return
- Array::with_capacity(10)
- [i32]
- Array::new(10)
- [i32; 10]
Q10. What syntax is required to take a mutable reference to T, when used within a function argument?
fn increment(i: T) {
// body elided
}
- *mut T
- mut ref T
- mut &T
- &mut T
Q11. The smart pointers Rc and Arc provide reference counting. What is the API for incrementing a reference count?
- .add()
- .incr()
- .clone()
- .increment()
- The error is reported and execution continues.
- An exception is raised. The effect(s) of the exception are defined by the error! macro.
- The program panics immediately.
- Rust attempts to convert the error to the local function's error type and return it as Result::Err. If that fails, the program panics.
-
/*
-
#
-
//!
-
//
-
.ignore()
-
an underscore (_)
- ..
- skip
- function that ends the lifetime of one of its arguments
- struct that contains a reference to a value
- function with a generic argument
- struct that contains a reference to a boxed value
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);
};
}
- [x]
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(())
}
- [x]
write!(&mut v, "{}{}{}", a, b, c)?;
- [ ]
v.write(a)?;
v.write(b)?;
v.write(c)?;
- [ ]
v.write(a, b, c)?;
- [ ]
v.write_all(a.as_bytes())?;
v.write_all(&b.to_string().as_bytes())?;
c.encode_utf8(&mut v);
Answered in rust user forum reference
fn main() {
let Some(x) = some_option_value;
}
- The code does not compile.
let
statements require a refutable pattern. Addif
beforelet
. - The code compiles.
let
statements sometimes require a refutable pattern. - The code does not compile.
let
statements requires an irrefutable pattern. Addif
beforelet
. - The code compiles.
let
do not require a refutable pattern.
- Lifetimes were redundantly specified in previous version of Rust.
- Lifetimes are specified when a struct is holding a reference to a value.
- Lifetimes are specified when certain values must outlive others.
- Lifetimes are always inferred by the compiler.
Q20. When used as a return type, which Rust type plays a similar role to Python's None
, JavaScript's null
, or the void
type in C/C++?
-
!
-
None
-
Null
-
()
-
.as_option()
-
.ok()
-
.to_option()
-
.into()
-
Copy
is enabled for primitive, built-in types. - Without
Copy
, Rust applies move semantics to a type's access. - When using
Clone
, copying data is explicit. - Until a type implements either
Copy
orClone
, its internal data cannot be copied.
fn returns_closure() -> dyn Fn(i32) -> i32 {
|x| x + 1
}
- The returned
fn
pointer and value need to be represented by another trait. - Closures are types, so they cannot be returned directly from a function.
- Closures are types and can be returned only if the concrete trait is implemented.
- Closures are represented by traits, so they cannot be a return type.
-
Arc<T>
-
Box<T>
- Both
Arc<T>
andRc<T>
are multithread safe. -
Rc<T>
- zero-sized types
- structs
- trait objects
- floating-point numbers
fn main() {
let c = 'z';
let heart_eyed_cat = '😻';
}
- Both are character literals.
-
heart_eyed_cat
is an invalid expression. -
c
is a string literal andheart_eyed_cat
is a character literal. - Both are string literals.
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?
-
Mutex<Arc<T>>
-
Rc<Mutex<T>>
-
Arc<Mutex<T>>
-
Mutex<Rc<T>>
let a = "a".to_string();
let b = "b".to_string();
let c = "c".to_string();
-
String::from(a,b,c)
-
format!("{}{}{}", a, b, c)
-
concat(a,b,c)
-
a + b + c
use std::fmt::Debug;
fn report<T:Debug>(a: &T) {
eprintln!("info: {:?}", a);
}
- read-only
- read/write
- debug
-
loop
-
for
-
while
-
do
enum Status {
Waiting,
Busy,
Error(String),
}
-
let s = Enum::new(Status::Waiting);
-
let s = new Status::Waiting;
-
let s = Status::Waiting;
-
let s = Status::new(Waiting);
- Enums are useful in matching patterns.
- Option is an enum type.
- Enum variants can have different types with associated data.
- the term enum is short for enummap