Rust (Programming Language)
Q1. Which type cast preserves the mathematical value in all cases?
Q2. What do the vertical bars represent here?
str:: thread:: spawn ( || {
println ! ( "LinkedIn" ) ;
} ) ;
reference
Q3. Which choice is not a scalar data type?
Q4. _ cannot be destructured.
reference
Q5. Which cargo
command checks a program for error without creating a binary executable?
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?
Q7. What is an alternative way of writing slice
that produces the same result?
...
let s = String :: form ( "hello" ) ;
let slice = & s[ 0 ..2 ] ;
Q8. Using the ?
operator at the end of an expression is equivalent to _ .
Q9. Which is valid syntax for defining an array of i32 values?
Q10. What syntax is required to take a mutable reference to T, when used within a function argument?
fn increment ( i : T ) {
// body elided
}
Q11. The smart pointers Rc and Arc provide reference counting. What is the API for incrementing a reference count?
reference
Q12. What happens when an error occurs that is being handled by the question mark (?) operator?
Q13. Which comment syntax is not legal?
Q14. In matching patterns, values are ignored with _.
Q15. Defining a _ requires a lifetime parameter.
Rust book reference
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 ) ;
}
reference
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 ( ( ) )
}
write ! ( & mut v, "{}{}{}" , a, b, c) ?;
v. write ( a) ?;
v. write ( b) ?;
v. write ( 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
Q18. Does the main
function compile? If so, why? If not, what do you need to change?
fn main ( ) {
let Some ( x) = some_option_value;
}
Q19. Which statement about lifetimes is false?
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++?
Q21. To convert a Result
to an Option
, which method should you use?
Q22. Which statement about the Clone
and Copy
traits is false?
ref from stack overflow
Q23. Why does this code not compile?
fn returns_closure ( ) -> dyn Fn ( i32 ) -> i32 {
|x| x + 1
}
Rust book reference
Q24. What smart pointer is used to allow multiple ownership of a value in various threads?
Rust book reference
Q25. Which types are not allowed within an enum variant's body?
Reference
Q26. Which statement about this code is true?
fn main ( ) {
let c = 'z' ;
let heart_eyed_cat = '😻' ;
}
Reference
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?
Rust book reference
Q28. Which idiom can be used to concatenate the strings a
, b
, c
?
let a = "a" . to_string ( ) ;
let b = "b" . to_string ( ) ;
let c = "c" . to_string ( ) ;
Q29. In this function. what level of access is provided to the variable a
?
use std:: fmt:: Debug ;
fn report < T : Debug > ( a : & T ) {
eprintln ! ( "info: {:?}" , a) ;
}
Q30. Which choice is not valid loop syntax?
Q31. How do you construct a value of Status
that is initialized to Waiting
?
enum Status {
Waiting ,
Busy ,
Error ( String ) ,
}
Q32. Which statement about enums is false?
Q33. What does an underscore (_) indicate when used as pattern?
Q34. What is a safe operation on a std::cell:UnsafeCell<T>
?
Reference
Q35. Generics are useful when you _ .
Q36. How do you create a Rust project on the command-line?
Reference
Q38. what is one of the roles of the let keyword?
let text = String :: new ( "LinkedIn" ) ;
Reference
Q39. How is a new enum initialized?
enum Option_i32 {
Some ( i32 ) ,
None ,
}
Reference
Q40. What are the main difference between const and static?
Reference
Q41. Which Rust data type represents a signed integer that has the same width as a pointer of the compile target's CPU?
Reference
Q42. When are supertraits needed?
Reference
Q43. Which types are legal for x to be in this snippet?
Reference
Q44. How do you access the married data in this struct?
struct person = Person {
height : u64 ,
weight : u64 ,
married : bool
}
Reference
Q45. To mark a function as visible to other crates, what do you need to do to its definition?
Reference
Q46. Which choice is a compound data type?
Reference
Q47. How could you make this function compile?
fn main ( ) {
let x = 5 ;
println ! ( "The value of x is: {}" , x) ;
x = 6 ;
println ! ( "The value of x is: {}" , x) ;
}
Reference
Q48. Using .unwrap() will _.
Reference
Q49. When should the panic! macro be called instead of using std::result::Result?
Reference
Q50. Which statement about arrays is true?
Reference
Q51. How would you select the value 2.0 from this tuple?
let pt = Point2D ( -1.0 , 2.0 )
Reference
Q52. When writing tests, which macro should you use to assert equality between two values?
Reference