Progress: finished primitive types, vecs, movesematics. progress structs

This commit is contained in:
2026-07-10 23:35:48 +02:00
parent 1c85acbd7e
commit ab5b9bff14
15 changed files with 92 additions and 12 deletions
+49
View File
@@ -0,0 +1,49 @@
#[derive(Debug)]
struct Order {
name: String,
year: u32,
made_by_phone: bool,
made_by_mobile: bool,
made_by_email: bool,
item_number: u32,
count: u32,
}
fn create_order_template() -> Order {
Order {
name: String::from("Bob"),
year: 2019,
made_by_phone: false,
made_by_mobile: false,
made_by_email: true,
item_number: 123,
count: 0,
}
}
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// let your_order =
let your_order = ;
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
assert_eq!(your_order.made_by_email, order_template.made_by_email);
assert_eq!(your_order.item_number, order_template.item_number);
assert_eq!(your_order.count, 1);
}
}
@@ -17,6 +17,7 @@ fn main() {
// Try a letter, try a digit (in single quotes), try a special character, try a character // Try a letter, try a digit (in single quotes), try a special character, try a character
// from a different language than your own, try an emoji 😉 // from a different language than your own, try an emoji 😉
// let your_character = ''; // let your_character = '';
let your_character = '僕';
if your_character.is_alphabetic() { if your_character.is_alphabetic() {
println!("Alphabetical!"); println!("Alphabetical!");
@@ -2,6 +2,14 @@ fn main() {
// TODO: Create an array called `a` with at least 100 elements in it. // TODO: Create an array called `a` with at least 100 elements in it.
// let a = ??? // let a = ???
let a = [
1, 2, 3, 4, 5, 6, 7, 1, 231, 234, 11, 4512, 51, 5, 12, 51, 25, 12, 45, 134, 12, 412, 45,
12, 512, 512, 5, 12, 512, 4, 124, 12, 5, 125, 123, 3412, 45, 123, 5123, 5, 12345, 123, 412,
56, 1236, 234, 6345, 6534, 6, 547, 456, 754, 6, 345, 34, 534, 6, 235, 123, 4523, 4523, 5,
235, 235, 2, 532, 523, 23, 124, 21, 2, 3, 51235, 664, 3466, 345,312,312,421,412,45,1245,123,423,523,56,234,5234,5234,5,346,345,6745,7456,7456,7,456,7456,7,4, 2, 6, 23, 12, 4512, 512,
5, 512, 5, 125, 125, 125, 12, 56, 75, 7456, 87,
];
if a.len() >= 100 { if a.len() >= 100 {
println!("Wow, that's a big array!"); println!("Wow, that's a big array!");
} else { } else {
@@ -11,6 +11,7 @@ mod tests {
// TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes. // TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes.
// let nice_slice = ??? // let nice_slice = ???
let nice_slice = &a[1..4];
assert_eq!([2, 3, 4], nice_slice); assert_eq!([2, 3, 4], nice_slice);
} }
} }
@@ -4,5 +4,7 @@ fn main() {
// TODO: Destructure the `cat` tuple in one statement so that the println works. // TODO: Destructure the `cat` tuple in one statement so that the println works.
// let /* your pattern here */ = cat; // let /* your pattern here */ = cat;
let (name, age) = cat;
println!("{name} is {age} years old"); println!("{name} is {age} years old");
} }
@@ -4,6 +4,7 @@ fn main() {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn indexing_tuple() { fn indexing_tuple() {
let numbers = (1, 2, 3); let numbers = (1, 2, 3);
@@ -11,6 +12,7 @@ mod tests {
// TODO: Use a tuple index to access the second element of `numbers` // TODO: Use a tuple index to access the second element of `numbers`
// and assign it to a variable called `second`. // and assign it to a variable called `second`.
// let second = ???; // let second = ???;
let second = numbers.1;
assert_eq!(second, 2, "This is not the 2nd number in the tuple!"); assert_eq!(second, 2, "This is not the 2nd number in the tuple!");
} }
+2
View File
@@ -1,6 +1,8 @@
fn elems_to_vec(a: i32, b: i32, c: i32) -> Vec<i32> { fn elems_to_vec(a: i32, b: i32, c: i32) -> Vec<i32> {
// TODO: Return a vector containing the elements a, b and c (in this order). // TODO: Return a vector containing the elements a, b and c (in this order).
// Use the "vec!" macro. // Use the "vec!" macro.
let vector: Vec<i32> = vec![a, b, c];
vector
} }
fn main() { fn main() {
+2
View File
@@ -4,6 +4,8 @@ fn vec_loop(input: &[i32]) -> Vec<i32> {
for element in input { for element in input {
// TODO: Multiply each element in the `input` slice by 2 and push it to // TODO: Multiply each element in the `input` slice by 2 and push it to
// the `output` vector. // the `output` vector.
output.push(element * 2);
} }
output output
@@ -1,6 +1,6 @@
// TODO: Fix the compiler error in this function. // TODO: Fix the compiler error in this function.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec; let mut vec = vec;
vec.push(88); vec.push(88);
@@ -20,7 +20,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
@@ -10,8 +10,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);
} }
+11 -4
View File
@@ -1,9 +1,12 @@
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?
red: u8,
green: u8,
blue: u8,
} }
struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */); struct ColorTupleStruct(u8, u8, u8);
#[derive(Debug)] #[derive(Debug)]
struct UnitStruct; struct UnitStruct;
@@ -20,7 +23,11 @@ mod tests {
fn regular_structs() { fn regular_structs() {
// TODO: Instantiate a regular struct. // TODO: Instantiate a regular struct.
// let green = // let green =
let green = ColorRegularStruct {
red: 0,
green: 255,
blue: 0,
};
assert_eq!(green.red, 0); assert_eq!(green.red, 0);
assert_eq!(green.green, 255); assert_eq!(green.green, 255);
assert_eq!(green.blue, 0); assert_eq!(green.blue, 0);
@@ -30,7 +37,7 @@ mod tests {
fn tuple_structs() { fn tuple_structs() {
// TODO: Instantiate a tuple struct. // TODO: Instantiate a tuple struct.
// let green = // let green =
let green = ColorTupleStruct(0, 255, 0);
assert_eq!(green.0, 0); assert_eq!(green.0, 0);
assert_eq!(green.1, 255); assert_eq!(green.1, 255);
assert_eq!(green.2, 0); assert_eq!(green.2, 0);
@@ -39,7 +46,7 @@ mod tests {
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit struct. // TODO: Instantiate a unit struct.
// let unit_struct = let unit_struct = UnitStruct;
let message = format!("{unit_struct:?}s are fun!"); let message = format!("{unit_struct:?}s are fun!");
assert_eq!(message, "UnitStructs are fun!"); assert_eq!(message, "UnitStructs are fun!");
+6
View File
@@ -36,6 +36,12 @@ mod tests {
// TODO: Create your own order using the update syntax and template above! // TODO: Create your own order using the update syntax and template above!
// let your_order = // let your_order =
let your_order = Order {
name: "Hacker in Rust".to_string(),
count: 1,
..order_template
};
assert_eq!(your_order.name, "Hacker in Rust"); assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year); assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone); assert_eq!(your_order.made_by_phone, order_template.made_by_phone);