rust Flashcards
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
describe these lines of code:
let n1: i64 = 7;
let v1: Vec<i64> = Vec::from([1, 2, 3, 4]);
let n2 = n1;
let v2 = v1;
println!("n1: {:?}", n1);
println!("n2: {:?}", n2);
println!("v1: {:?}", v1);
println!("v2: {:?}", v2);</i64>
let n1: i64 = 7;
let v1: Vec<i64> = Vec::from([1, 2, 3, 4]);
let n2 = n1; // implicitly a copy, because i64 is Copy
let v2 = v1; // *implicitly a move*, because Vec is not Copy
println!("n1: {:?}", n1);
println!("n2: {:?}", n2);
//println!("v1: {:?}", v1); // "borrow of moved value: `v1`"
println!("v2: {:?}", v2);</i64>
how to implement copy
[derive(Copy, Clone)]
Summary:
- derive from Copy, Clone
- or, call clone on the fields
struct MyStruct;
- struct MyStruct {
field1: String,
field2: Vec<u64>,
}</u64>
impl Clone for MyStruct {
fn clone(&self) -> Self {
MyStruct {
field1: self.field1.clone(),
field2: self.field2.clone(),
}
}
}
why is it hard to implement copy trait for a vector?
The Vec keeps most of its data on the heap: copying its stack value would create multiple references to that heap memory.
which is implicit:
cloning or copying
copying is implicit but cloning is not
what does #[derive(Debug, Clone)] do
The #[derive…] line gets us free implementations of Debug (printing with {:?}) and Clone (a .clone() method).
how to initialize a struct so that the struct is modifiable
let mut p = GeoPoint {…}
how to implement the following function in struct
fn antipode(&self) -> GeoPoint {}
impl GeoPoint {
fn antipode(&self) -> GeoPoint {
GeoPoint {
lat: -self.lat,
lon: -self.lon,
ele: self.ele,
}
}
}
what is equivalent to this:
fn antipode(&self) -> GeoPoint {…}
fn antipode(self: &GeoPoint) -> GeoPoint {…}
what are the 3 receiving arguments for a struct. and what are they used for
- the value itself (self): when the struct is no longer needed
- or a reference (&self):
- a mutable reference (&mut self): change a structs own content
what is an associated function
If we implement a function on a type that does not have a receiver argument (self), it is an associated function (≈ static method).
how do you access associated functions
Associated functions are accessed from the type with ::
let p = GeoPoint::new(49.267, -122.967);