From 201a015f87d4b8d1ef20e02ba95c58eeb434dd65 Mon Sep 17 00:00:00 2001 From: Julian Mayer Date: Thu, 11 Dec 2025 10:42:44 +0100 Subject: [PATCH] Solutions to the first exercises --- .idea/.gitignore | 8 ++++++++ exercises/01_intro/00_welcome/src/lib.rs | 2 +- exercises/01_intro/01_syntax/src/lib.rs | 2 +- exercises/02_basic_calculator/00_intro/src/lib.rs | 2 +- exercises/02_basic_calculator/01_integers/src/lib.rs | 2 +- exercises/02_basic_calculator/02_variables/src/lib.rs | 1 + exercises/02_basic_calculator/03_if_else/src/lib.rs | 10 +++++++++- 7 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..13566b81b0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/exercises/01_intro/00_welcome/src/lib.rs b/exercises/01_intro/00_welcome/src/lib.rs index ec95b6ca75..e0f1fc2653 100644 --- a/exercises/01_intro/00_welcome/src/lib.rs +++ b/exercises/01_intro/00_welcome/src/lib.rs @@ -17,7 +17,7 @@ // You can also find solutions to all exercises in the `solutions` git branch. fn greeting() -> &'static str { // TODO: fix me 👇 - "I'm ready to __!" + "I'm ready to learn Rust!" } // Your solutions will be automatically verified by a set of tests. diff --git a/exercises/01_intro/01_syntax/src/lib.rs b/exercises/01_intro/01_syntax/src/lib.rs index 676d81b22e..535b6dfda4 100644 --- a/exercises/01_intro/01_syntax/src/lib.rs +++ b/exercises/01_intro/01_syntax/src/lib.rs @@ -3,7 +3,7 @@ // partner in this course and it'll often guide you in the right direction! // // The input parameters should have the same type of the return type. -fn compute(a, b) -> u32 { +fn compute(a: u32, b: u32) -> u32 { // Don't touch the function body. a + b * 2 } diff --git a/exercises/02_basic_calculator/00_intro/src/lib.rs b/exercises/02_basic_calculator/00_intro/src/lib.rs index 03aeb16339..d65a5cd207 100644 --- a/exercises/02_basic_calculator/00_intro/src/lib.rs +++ b/exercises/02_basic_calculator/00_intro/src/lib.rs @@ -1,6 +1,6 @@ fn intro() -> &'static str { // TODO: fix me 👇 - "I'm ready to __!" + "I'm ready to build a calculator in Rust!" } #[cfg(test)] diff --git a/exercises/02_basic_calculator/01_integers/src/lib.rs b/exercises/02_basic_calculator/01_integers/src/lib.rs index a87b56fba3..4eb97f56ee 100644 --- a/exercises/02_basic_calculator/01_integers/src/lib.rs +++ b/exercises/02_basic_calculator/01_integers/src/lib.rs @@ -1,6 +1,6 @@ fn compute(a: u32, b: u32) -> u32 { // TODO: change the line below to fix the compiler error and make the tests pass. - let multiplier: u8 = 4; + let multiplier: u32 = 4; a + b * multiplier } diff --git a/exercises/02_basic_calculator/02_variables/src/lib.rs b/exercises/02_basic_calculator/02_variables/src/lib.rs index e8d116749a..ddf74e67a5 100644 --- a/exercises/02_basic_calculator/02_variables/src/lib.rs +++ b/exercises/02_basic_calculator/02_variables/src/lib.rs @@ -8,6 +8,7 @@ pub fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 { // TODO: define a variable named `distance` with the right value to get tests to pass // Do you need to annotate the type of `distance`? Why or why not? + let distance: u32 = end - start; // Don't change the line below distance / time_elapsed diff --git a/exercises/02_basic_calculator/03_if_else/src/lib.rs b/exercises/02_basic_calculator/03_if_else/src/lib.rs index fa2e06ea76..a309ddf9e1 100644 --- a/exercises/02_basic_calculator/03_if_else/src/lib.rs +++ b/exercises/02_basic_calculator/03_if_else/src/lib.rs @@ -2,7 +2,15 @@ /// `13` if `n` is divisible by `3`, /// `17` otherwise. fn magic_number(n: u32) -> u32 { - todo!() + if n % 2 == 0 { + return 12; + } + + if n % 3 == 0 { + return 13; + } + + 17 } #[cfg(test)]