exercises 06 done, onto structs
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
DON'T EDIT THIS FILE!
|
DON'T EDIT THIS FILE!
|
||||||
|
|
||||||
move_semantics1
|
structs1
|
||||||
|
|
||||||
intro1
|
intro1
|
||||||
intro2
|
intro2
|
||||||
@@ -27,3 +27,8 @@ primitive_types5
|
|||||||
primitive_types6
|
primitive_types6
|
||||||
vecs1
|
vecs1
|
||||||
vecs2
|
vecs2
|
||||||
|
move_semantics1
|
||||||
|
move_semantics2
|
||||||
|
move_semantics3
|
||||||
|
move_semantics4
|
||||||
|
move_semantics5
|
||||||
@@ -8,7 +8,8 @@ fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = vec![22, 44, 66];
|
let vec0 = vec![22, 44, 66];
|
||||||
let vec1 = fill_vec(vec0);
|
let vec1 = fill_vec(vec0.clone());
|
||||||
|
println!("{:?}", vec0);
|
||||||
println!("{:?}", vec1);
|
println!("{:?}", vec1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ mod tests {
|
|||||||
fn move_semantics2() {
|
fn move_semantics2() {
|
||||||
let vec0 = vec![22, 44, 66];
|
let vec0 = vec![22, 44, 66];
|
||||||
|
|
||||||
let vec1 = fill_vec(vec0);
|
let vec1 = fill_vec(vec0.clone());
|
||||||
|
|
||||||
assert_eq!(vec0, [22, 44, 66]);
|
assert_eq!(vec0, [22, 44, 66]);
|
||||||
assert_eq!(vec1, [22, 44, 66, 88]);
|
assert_eq!(vec1, [22, 44, 66, 88]);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// TODO: Fix the compiler error in the function without adding any new line.
|
// TODO: Fix the compiler error in the function without adding any new line.
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
|
||||||
vec.push(88);
|
vec.push(88);
|
||||||
|
|
||||||
vec
|
vec
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
fn main() {
|
fn main() {}
|
||||||
// You can optionally experiment here.
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@@ -10,8 +8,8 @@ mod tests {
|
|||||||
fn move_semantics4() {
|
fn move_semantics4() {
|
||||||
let mut x = Vec::new();
|
let mut x = Vec::new();
|
||||||
let y = &mut x;
|
let y = &mut x;
|
||||||
let z = &mut x;
|
|
||||||
y.push(42);
|
y.push(42);
|
||||||
|
let z = &mut x;
|
||||||
z.push(13);
|
z.push(13);
|
||||||
assert_eq!(x, [42, 13]);
|
assert_eq!(x, [42, 13]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
// removing references (the character `&`).
|
// removing references (the character `&`).
|
||||||
|
|
||||||
// Shouldn't take ownership
|
// Shouldn't take ownership
|
||||||
fn get_char(data: String) -> char {
|
fn get_char(data: &String) -> char {
|
||||||
data.chars().last().unwrap()
|
data.chars().last().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should take ownership
|
// Should take ownership
|
||||||
fn string_uppercase(mut data: &String) {
|
fn string_uppercase(mut data: String) {
|
||||||
data = data.to_uppercase();
|
data = data.to_uppercase();
|
||||||
|
|
||||||
println!("{data}");
|
println!("{data}");
|
||||||
@@ -18,7 +18,7 @@ fn string_uppercase(mut data: &String) {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let data = "Rust is great!".to_string();
|
let data = "Rust is great!".to_string();
|
||||||
|
|
||||||
get_char(data);
|
get_char(&data);
|
||||||
|
|
||||||
string_uppercase(&data);
|
string_uppercase(data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
struct ColorRegularStruct {
|
struct ColorRegularStruct {
|
||||||
// TODO: Add the fields that the test `regular_structs` expects.
|
// TODO: Add the fields that the test `regular_structs` expects.
|
||||||
// What types should the fields have? What are the minimum and maximum values for RGB colors?
|
// What types should the fields have? What are the minimum and maximum values for RGB colors?
|
||||||
|
green: i32,
|
||||||
|
red: i32,
|
||||||
|
blue: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
|
#[derive(debug)]
|
||||||
|
struct ColorTupleStruct(i32, i32, i32);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UnitStruct;
|
struct UnitStruct;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
let color = ColorTupleStruct(12, 55, 200);
|
||||||
|
println!("{:?}", color)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Binary file not shown.
@@ -1,4 +1,28 @@
|
|||||||
fn main() {
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
let mut vec = vec;
|
||||||
// It will be automatically filled after you finish the exercise.
|
|
||||||
|
vec.push(88);
|
||||||
|
|
||||||
|
vec
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn move_semantics2() {
|
||||||
|
let vec0 = vec![22, 44, 66];
|
||||||
|
|
||||||
|
// Cloning `vec0` so that the clone is moved into `fill_vec`, not `vec0`
|
||||||
|
// itself.
|
||||||
|
let vec1 = fill_vec(vec0.clone());
|
||||||
|
|
||||||
|
assert_eq!(vec0, [22, 44, 66]);
|
||||||
|
assert_eq!(vec1, [22, 44, 66, 88]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,22 @@
|
|||||||
fn main() {
|
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
// ^^^ added
|
||||||
// It will be automatically filled after you finish the exercise.
|
vec.push(88);
|
||||||
|
|
||||||
|
vec
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn move_semantics3() {
|
||||||
|
let vec0 = vec![22, 44, 66];
|
||||||
|
let vec1 = fill_vec(vec0);
|
||||||
|
assert_eq!(vec1, [22, 44, 66, 88]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,19 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
// You can optionally experiment here.
|
||||||
// It will be automatically filled after you finish the exercise.
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn move_semantics4() {
|
||||||
|
let mut x = Vec::new();
|
||||||
|
let y = &mut x;
|
||||||
|
// `y` used here.
|
||||||
|
y.push(42);
|
||||||
|
// The mutable reference `y` is not used anymore,
|
||||||
|
// therefore a new reference can be created.
|
||||||
|
let z = &mut x;
|
||||||
|
z.push(13);
|
||||||
|
assert_eq!(x, [42, 13]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,23 @@
|
|||||||
fn main() {
|
#![allow(clippy::ptr_arg)]
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
|
||||||
// It will be automatically filled after you finish the exercise.
|
// Borrows instead of taking ownership.
|
||||||
|
// It is recommended to use `&str` instead of `&String` here. But this is
|
||||||
|
// enough for now because we didn't handle strings yet.
|
||||||
|
fn get_char(data: &String) -> char {
|
||||||
|
data.chars().last().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Takes ownership instead of borrowing.
|
||||||
|
fn string_uppercase(mut data: String) {
|
||||||
|
data = data.to_uppercase();
|
||||||
|
|
||||||
|
println!("{data}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let data = "Rust is great!".to_string();
|
||||||
|
|
||||||
|
get_char(&data);
|
||||||
|
|
||||||
|
string_uppercase(data);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user