exercises 06 done, onto structs

This commit is contained in:
2026-06-17 16:13:28 +02:00
parent 6baf967df0
commit 8031b2858f
11 changed files with 112 additions and 27 deletions
@@ -8,7 +8,8 @@ fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
fn main() {
let vec0 = vec![22, 44, 66];
let vec1 = fill_vec(vec0);
let vec1 = fill_vec(vec0.clone());
println!("{:?}", vec0);
println!("{:?}", vec1);
}
@@ -22,7 +23,7 @@ mod tests {
fn move_semantics2() {
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!(vec1, [22, 44, 66, 88]);
@@ -1,5 +1,5 @@
// 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
@@ -1,6 +1,4 @@
fn main() {
// You can optionally experiment here.
}
fn main() {}
#[cfg(test)]
mod tests {
@@ -10,8 +8,8 @@ mod tests {
fn move_semantics4() {
let mut x = Vec::new();
let y = &mut x;
let z = &mut x;
y.push(42);
let z = &mut x;
z.push(13);
assert_eq!(x, [42, 13]);
}
@@ -4,12 +4,12 @@
// removing references (the character `&`).
// Shouldn't take ownership
fn get_char(data: String) -> char {
data.chars().last().unwrap()
fn get_char(data: &String) -> char {
data.chars().last().unwrap();
}
// Should take ownership
fn string_uppercase(mut data: &String) {
fn string_uppercase(mut data: String) {
data = data.to_uppercase();
println!("{data}");
@@ -18,7 +18,7 @@ fn string_uppercase(mut data: &String) {
fn main() {
let data = "Rust is great!".to_string();
get_char(data);
get_char(&data);
string_uppercase(&data);
string_uppercase(data);
}
+7 -2
View File
@@ -1,15 +1,20 @@
struct ColorRegularStruct {
// 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?
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)]
struct UnitStruct;
fn main() {
// You can optionally experiment here.
let color = ColorTupleStruct(12, 55, 200);
println!("{:?}", color)
}
#[cfg(test)]