From 8f0234e3a297d99c958e8b85e131ec9323cd53c7 Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Fri, 18 Sep 2026 16:30:30 -0500 Subject: rustlings: complete 04 --- .../04_primitive_types/primitive_types1.rs | 4 +--- .../04_primitive_types/primitive_types2.rs | 7 +------ .../04_primitive_types/primitive_types3.rs | 3 +-- .../04_primitive_types/primitive_types4.rs | 3 +-- .../04_primitive_types/primitive_types5.rs | 3 +-- .../04_primitive_types/primitive_types6.rs | 4 +--- .../04_primitive_types/primitive_types1.rs | 11 +++++++++-- .../04_primitive_types/primitive_types2.rs | 21 ++++++++++++++++++-- .../04_primitive_types/primitive_types3.rs | 11 +++++++++-- .../04_primitive_types/primitive_types4.rs | 23 ++++++++++++++++++++-- .../04_primitive_types/primitive_types5.rs | 8 ++++++-- .../04_primitive_types/primitive_types6.rs | 16 +++++++++++++-- 12 files changed, 84 insertions(+), 30 deletions(-) diff --git a/rustlings/exercises/04_primitive_types/primitive_types1.rs b/rustlings/exercises/04_primitive_types/primitive_types1.rs index 84923c7..778c0f6 100644 --- a/rustlings/exercises/04_primitive_types/primitive_types1.rs +++ b/rustlings/exercises/04_primitive_types/primitive_types1.rs @@ -6,9 +6,7 @@ fn main() { println!("Good morning!"); } - // TODO: Define a boolean variable with the name `is_evening` before the `if` statement below. - // The value of the variable should be the negation (opposite) of `is_morning`. - // let … + let is_evening = !is_morning; if is_evening { println!("Good evening!"); } diff --git a/rustlings/exercises/04_primitive_types/primitive_types2.rs b/rustlings/exercises/04_primitive_types/primitive_types2.rs index 1401847..a45f84d 100644 --- a/rustlings/exercises/04_primitive_types/primitive_types2.rs +++ b/rustlings/exercises/04_primitive_types/primitive_types2.rs @@ -12,12 +12,7 @@ fn main() { println!("Neither alphabetic nor numeric!"); } - // TODO: Analogous to the example before, declare a variable called `your_character` - // below with your favorite 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 😉 - // let your_character = ''; - + let your_character = '😉'; if your_character.is_alphabetic() { println!("Alphabetical!"); } else if your_character.is_numeric() { diff --git a/rustlings/exercises/04_primitive_types/primitive_types3.rs b/rustlings/exercises/04_primitive_types/primitive_types3.rs index 9b79c0c..c5a259d 100644 --- a/rustlings/exercises/04_primitive_types/primitive_types3.rs +++ b/rustlings/exercises/04_primitive_types/primitive_types3.rs @@ -1,6 +1,5 @@ fn main() { - // TODO: Create an array called `a` with at least 100 elements in it. - // let a = ??? + let a = [0; 1000]; if a.len() >= 100 { println!("Wow, that's a big array!"); diff --git a/rustlings/exercises/04_primitive_types/primitive_types4.rs b/rustlings/exercises/04_primitive_types/primitive_types4.rs index 16e4fd9..81af3e2 100644 --- a/rustlings/exercises/04_primitive_types/primitive_types4.rs +++ b/rustlings/exercises/04_primitive_types/primitive_types4.rs @@ -8,8 +8,7 @@ mod tests { fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; - // TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes. - // let nice_slice = ??? + let nice_slice = &a[1..4]; assert_eq!([2, 3, 4], nice_slice); } diff --git a/rustlings/exercises/04_primitive_types/primitive_types5.rs b/rustlings/exercises/04_primitive_types/primitive_types5.rs index 6e00ef5..a3e60d8 100644 --- a/rustlings/exercises/04_primitive_types/primitive_types5.rs +++ b/rustlings/exercises/04_primitive_types/primitive_types5.rs @@ -1,8 +1,7 @@ fn main() { let cat = ("Furry McFurson", 3.5); - // TODO: Destructure the `cat` tuple in one statement so that the println works. - // let /* your pattern here */ = cat; + let (name, age) = cat; println!("{name} is {age} years old"); } diff --git a/rustlings/exercises/04_primitive_types/primitive_types6.rs b/rustlings/exercises/04_primitive_types/primitive_types6.rs index a97e531..dec639a 100644 --- a/rustlings/exercises/04_primitive_types/primitive_types6.rs +++ b/rustlings/exercises/04_primitive_types/primitive_types6.rs @@ -8,9 +8,7 @@ mod tests { fn indexing_tuple() { let numbers = (1, 2, 3); - // TODO: Use a tuple index to access the second element of `numbers` - // and assign it to a variable called `second`. - // let second = ???; + let second = numbers.1; assert_eq!(second, 2, "This is not the 2nd number in the tuple!"); } diff --git a/rustlings/solutions/04_primitive_types/primitive_types1.rs b/rustlings/solutions/04_primitive_types/primitive_types1.rs index dcf2377..fac6ec0 100644 --- a/rustlings/solutions/04_primitive_types/primitive_types1.rs +++ b/rustlings/solutions/04_primitive_types/primitive_types1.rs @@ -1,4 +1,11 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + let is_morning = true; + if is_morning { + println!("Good morning!"); + } + + let is_evening = !is_morning; + if is_evening { + println!("Good evening!"); + } } diff --git a/rustlings/solutions/04_primitive_types/primitive_types2.rs b/rustlings/solutions/04_primitive_types/primitive_types2.rs index dcf2377..eecc680 100644 --- a/rustlings/solutions/04_primitive_types/primitive_types2.rs +++ b/rustlings/solutions/04_primitive_types/primitive_types2.rs @@ -1,4 +1,21 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + let my_first_initial = 'C'; + if my_first_initial.is_alphabetic() { + println!("Alphabetical!"); + } else if my_first_initial.is_numeric() { + println!("Numerical!"); + } else { + println!("Neither alphabetic nor numeric!"); + } + + // Example with an emoji. + let your_character = '🦀'; + + if your_character.is_alphabetic() { + println!("Alphabetical!"); + } else if your_character.is_numeric() { + println!("Numerical!"); + } else { + println!("Neither alphabetic nor numeric!"); + } } diff --git a/rustlings/solutions/04_primitive_types/primitive_types3.rs b/rustlings/solutions/04_primitive_types/primitive_types3.rs index dcf2377..8dd109f 100644 --- a/rustlings/solutions/04_primitive_types/primitive_types3.rs +++ b/rustlings/solutions/04_primitive_types/primitive_types3.rs @@ -1,4 +1,11 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // An array with 100 elements of the value 42. + let a = [42; 100]; + + if a.len() >= 100 { + println!("Wow, that's a big array!"); + } else { + println!("Meh, I eat arrays like that for breakfast."); + panic!("Array not big enough, more elements needed"); + } } diff --git a/rustlings/solutions/04_primitive_types/primitive_types4.rs b/rustlings/solutions/04_primitive_types/primitive_types4.rs index dcf2377..4807e66 100644 --- a/rustlings/solutions/04_primitive_types/primitive_types4.rs +++ b/rustlings/solutions/04_primitive_types/primitive_types4.rs @@ -1,4 +1,23 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + #[test] + fn slice_out_of_array() { + let a = [1, 2, 3, 4, 5]; + // 0 1 2 3 4 <- indices + // ------- + // | + // +--- slice + + // Note that the upper index 4 is excluded. + let nice_slice = &a[1..4]; + assert_eq!([2, 3, 4], nice_slice); + + // The upper index can be included by using the syntax `..=` (with `=` sign) + let nice_slice = &a[1..=3]; + assert_eq!([2, 3, 4], nice_slice); + } } diff --git a/rustlings/solutions/04_primitive_types/primitive_types5.rs b/rustlings/solutions/04_primitive_types/primitive_types5.rs index dcf2377..46d7ae8 100644 --- a/rustlings/solutions/04_primitive_types/primitive_types5.rs +++ b/rustlings/solutions/04_primitive_types/primitive_types5.rs @@ -1,4 +1,8 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + let cat = ("Furry McFurson", 3.5); + + // Destructuring the tuple. + let (name, age) = cat; + + println!("{name} is {age} years old"); } diff --git a/rustlings/solutions/04_primitive_types/primitive_types6.rs b/rustlings/solutions/04_primitive_types/primitive_types6.rs index dcf2377..9b7c277 100644 --- a/rustlings/solutions/04_primitive_types/primitive_types6.rs +++ b/rustlings/solutions/04_primitive_types/primitive_types6.rs @@ -1,4 +1,16 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + #[test] + fn indexing_tuple() { + let numbers = (1, 2, 3); + + // Tuple indexing syntax. + let second = numbers.1; + + assert_eq!(second, 2, "This is not the 2nd number in the tuple!"); + } } -- cgit v1.2.3