click below
click below
Normal Size Small Size show me how
RUST 1
| Question | Answer |
|---|---|
| What are the roles of rustup, rustc and the cargo commands? | Cargo: a packaging and build tool Rustup: toolchain management tool Rustc: just the compiler |
| How does rust ensure memory safety? | Ownership |
| Where are rust variables stored as the program executes? | Stack |
| Does the code compile? let x = 1; let mut y = 1; let x = “helllo” y= 3.14; | No because setting floating point value after initially setting it to an int |
| What are the types of x and y? let x = 1.0; let y = 3; | x is f64 y = i32 |
| What is the problem with this function? fn add_numbers(a, b) -> i32 { a + b } | No types for x and y in the parentheses of function (need :i32) |
| Does the code compile? let x = 1; let y = 2; let z = if x > y {x} else {y}; | The code compiles |
| Does the following code compile? let x = 1; let y = x; println!(“{},{}”, x, y); | The code compiles because i32 has a copy trait |
| Does the following code compile? let x = “hello”.to_string(); let y = x; println!(“{},{}”, x, y); | No. Absence of copy type so ownership results in no access to x anymore |
| What part of memory will hold the integer value (42) after executing this code? let x = Box::new(42); | Heap |
| What is the output of the following program? (...) fn main(){ let values = vec![1,2,3,4]; let result = sum(value); println!(“the sum of {:?} is {}”, values, result); } | Problem: values stops existing after let result line |
| What is the output of the following program? fn main() { let values = vec![“one”.to_string(), “two”.to_string()]; let x = values[values.len()-2]; println!(“{}, x) } | Doesn’t compile Dynamic data structures, tracks length at runtime so ownership can’t move data out of vector Can’t index vectors cause you don’t know the length for sure Cannot move out of index of ‘Vec<String>’ |
| What are ownership rules? | Each value in rust has an owner There can only be one owner at a time When the owner goes out of scope, the value will be dropped |
| What are the Borrow Rules? | At given given time, you can have either one mutable reference of any number of immutable references References must always be valid |
| What is the data type of s in the following example? fn main () { let s = &“hello”.to_string()[1..2]; println!(“{s}”); } | String slice: &str |
| Is this a valid struct definition? #derive(Debug, Clone, Copy)] struct person{ name: string; x = i32; } | No, you can't copy a string type so Copy trait can't be implemented |
| Where do you need to provide an irrefutable pattern for matching? | Variable assignment Parameters of function For loops |
| What is the data type of x let x = Some(12); | Option<i32> |
| What does Option::<T>::upwrap() do? | Panics |
| Change the line to make it work? fn sum(row: &str) -> Result<i32>, ParseIntError>{ let mut acc = 0; acc += token.parse::<i32>(); | acc += token.parse::<i32>()?; |
| What is the potential danger of creating a &str slice reference, like let s = &unknown_string[4..9] | If they’re unicode encoded so it may end up in the middle of character because there is variable length encoding Program would panic |
| String vs &str? | String owns the data, str always borrows the data |
| What is the data type of r? let mut v = vec![1,2,3,4]; let r = &mut x[1..3]; | r:&mut[i32] |
| What is the type and value of x? Let mut m = HashMap::new(); m.insert(“for”,”bar”); Let x = m.entry(“bar”).or_insert(“baz”); | x:&mut &str ->“baz” |
| What’s the problem with the following generic function? fn smaller<T>(a:T, b:T)->T{ if a <= b {a} else {b} } | The T data type needs to have the partial order trait, so that you can compare. fn smaller<T: PartialOrd>(a: T, b: T) -> T { if a <= b { a } else { b } } |
| What is wrong with the following generic type implementation block? Struct Vector<T>{ … } Impl<T> Vector<T>{ … } Trait Introspect { fn kind(&self)-> &str; } | Can’t implement kind |
| The following code doesn’t compile fn smaller<T>(x:&T, y: &T)->(...) | TICKS need to be added No lifetime specification |