rust Flashcards
(89 cards)
what type does the vector have to be to be able to call push on it
mutable
how can you make a copy of the variable n1?
let n4 = n1
what does this do?
let n2: &i32 = &n1;
a &i32 referring to n1
what does this do?
let n4 = n1;
// another i32 variable containing a copy of 1234
what will happen
let mut n1 = 1234_i64;
let n2 = &n1;
n1 = 4567;
compiles
what are three types of borrowing
use the original value;
have several immutable references to it;
have one mutable reference.
what will happen:
let mut n1 = 1234;
let n2 = &mut n1;
*n2 = 4567;
println!(“{}”, *n2);
println!(“{} {}”, n1, n2);
let mut n1 = 1234;
let n2 = &mut n1; // note: a mutable reference
*n2 = 4567;
println!(“{}”, *n2);
//println!(“{} {}”, n1, n2); // “cannot borrow n1
as immutable because it is also borrowed as mutable”
will this compile
let mut n1 = 1234;
{
let n2 = &mut n1;
*n2 = 4567;
println!(“{}”, *n2);
}
n1 = 7890;
println!(“{}”, n1);
let mut n1 = 1234;
{
let n2 = &mut n1;
*n2 = 4567;
println!(“{}”, *n2); // prints 4567
} // n2 is out of scope here, so no longer has a reference
n1 = 7890;
println!(“{}”, n1); // prints 7890
will this compile
let mut n1 = 1234;
let n2 = &mut n1;
let n3 = &mut n1;
yes
what happens when you pass a value to a function
When a value is passed to a function, ownership is given permanently to the function. Or, ownership moves to the function: is transferred permanently.
will this compile
sum_vec(nums: Vec<i64>) -> Vec<64> {}</i64>
let numbers = Vec::from([1, 2, 3, 4]);
println!(“{}”, sum_vec(numbers));
println!(“”, numbers);
let numbers = Vec::from([1, 2, 3, 4]);
println!(“{}”, sum_vec(numbers)); // prints 10
println!(“”, numbers);
borrow of moved value: numbers
how do you ask for references
fn some_func( val : &Vec<i64>) -> i64 { ...}</i64>
will this compile
fn sum_vec(values: &Vec<i64>) -> i64 {
values.iter().fold(0, |a, b| a + b)
}</i64>
let numbers = Vec::from([1, 2, 3, 4]);
println!(“{}”, sum_vec(&numbers));
println!(“”, numbers);
yes
question; what happens if i pass append_sum(mut val); ?
chatgpt it
will this compile:
let mut numbers = Vec::from([1, 2, 3, 4]);
append_sum(&mut numbers);
println!(“”, numbers);
let mut numbers = Vec::from([1, 2, 3, 4]);
append_sum(&mut numbers);
println!(“”, numbers); // prints [1, 2, 3, 4, 10]
what are the two ownership rules
- At any given time, you can have either one mutable reference or any number of immutable references.
- References must always be valid.
whats the function argument type for a mutable reference
&mut
will this work:
fn sum_vec(values: Vec<i64>) -> i64 {
values.iter().fold(0, |a, b| a + b)
}</i64>
fn main() {
let numbers = Vec::from([1, 2, 3, 4]);
let summed = sum_vec(numbers);
println!(“{}”, summed);
println!(“{}”, summed);
}
yes
will this work:
fn sum_vec(values: Vec<i64>) -> i64 {
values.iter().fold(0, |a, b| a + b)
}</i64>
fn main() {
let numbers = Vec::from([1, 2, 3, 4]);
let summed = sum_vec(numbers);
println!(“”, numbers);
}
no, because its given to sum_vec
will this work:
fn sum_vec(values: &Vec<i64>) -> i64 {
values.iter().fold(0, |a, b| a + b)
}
fn main() {
let numbers = Vec::from([1, 2, 3, 4]);
let summed = sum_vec(numbers);
println!("{:?}", numbers);
println!("{}", summed);
}</i64>
no, becuase numbers is not passed as a reference in the main function
will this work:
fn sum_vec(values: &Vec<i64>) -> i64 {
values.iter().fold(0, |a, b| a + b)
}
fn main() {
let numbers = Vec::from([1, 2, 3, 4]);
let summed = sum_vec(&numbers);
println!("{:?}", numbers);
println!("{}", summed);</i64>
yes
will this work:
fn print_int(n: i64) {
println!(“”, n);
}
fn print_vec(v: Vec<i64>) {
println!("{:?}", v);
}</i64>
let n: i64 = 7;
let v: Vec<i64> = Vec::from([1, 2, 3, 4]);
println!("n: {:?}", n);
println!("v: {:?}", v);
print_int(n);
print_vec(v);
println!("n: {:?}", n);
println!("v: {:?}", v);</i64>
only works for print n because Vec does not implement Copy trait
what are some requirements to automatically copy a value as a reference to a function
types that implement the copy trait
what are the types that have their ownership given away when they are assigned to a different variable?
non-copy types