From 226a48c93ad9ce05ae7b3075d5b4258eedef8d6e Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Fri, 18 Sep 2026 16:07:11 -0500 Subject: rustlings: complete 02 --- rustlings/.rustlings-state.txt | 7 ++++++- rustlings/exercises/02_functions/functions1.rs | 4 +++- rustlings/exercises/02_functions/functions2.rs | 3 +-- rustlings/exercises/02_functions/functions3.rs | 3 +-- rustlings/exercises/02_functions/functions4.rs | 7 +------ rustlings/exercises/02_functions/functions5.rs | 3 +-- rustlings/solutions/02_functions/functions1.rs | 8 ++++++-- rustlings/solutions/02_functions/functions2.rs | 11 +++++++++-- rustlings/solutions/02_functions/functions3.rs | 10 ++++++++-- rustlings/solutions/02_functions/functions4.rs | 17 +++++++++++++++-- rustlings/solutions/02_functions/functions5.rs | 9 +++++++-- 11 files changed, 58 insertions(+), 24 deletions(-) diff --git a/rustlings/.rustlings-state.txt b/rustlings/.rustlings-state.txt index b83f6a3..ce94351 100644 --- a/rustlings/.rustlings-state.txt +++ b/rustlings/.rustlings-state.txt @@ -1,6 +1,6 @@ DON'T EDIT THIS FILE! -functions1 +if1 intro1 intro2 @@ -10,3 +10,8 @@ variables3 variables4 variables5 variables6 +functions1 +functions2 +functions3 +functions4 +functions5 \ No newline at end of file diff --git a/rustlings/exercises/02_functions/functions1.rs b/rustlings/exercises/02_functions/functions1.rs index a812c21..5dbae07 100644 --- a/rustlings/exercises/02_functions/functions1.rs +++ b/rustlings/exercises/02_functions/functions1.rs @@ -1,4 +1,6 @@ -// TODO: Add some function with the name `call_me` without arguments or a return value. +fn call_me() { + println!("call_me()"); +} fn main() { call_me(); // Don't change this line diff --git a/rustlings/exercises/02_functions/functions2.rs b/rustlings/exercises/02_functions/functions2.rs index 2c773c6..27f2cc7 100644 --- a/rustlings/exercises/02_functions/functions2.rs +++ b/rustlings/exercises/02_functions/functions2.rs @@ -1,5 +1,4 @@ -// TODO: Add the missing type of the argument `num` after the colon `:`. -fn call_me(num:) { +fn call_me(num: u32) { for i in 0..num { println!("Ring! Call number {}", i + 1); } diff --git a/rustlings/exercises/02_functions/functions3.rs b/rustlings/exercises/02_functions/functions3.rs index 8d65477..88dff5b 100644 --- a/rustlings/exercises/02_functions/functions3.rs +++ b/rustlings/exercises/02_functions/functions3.rs @@ -5,6 +5,5 @@ fn call_me(num: u8) { } fn main() { - // TODO: Fix the function call. - call_me(); + call_me(3); } diff --git a/rustlings/exercises/02_functions/functions4.rs b/rustlings/exercises/02_functions/functions4.rs index b22bffd..2c5a3b0 100644 --- a/rustlings/exercises/02_functions/functions4.rs +++ b/rustlings/exercises/02_functions/functions4.rs @@ -1,14 +1,9 @@ -// This store is having a sale where if the price is an even number, you get 10 -// Rustbucks off, but if it's an odd number, it's 3 Rustbucks off. -// Don't worry about the function bodies themselves, we are only interested in -// the signatures for now. - fn is_even(num: i64) -> bool { num % 2 == 0 } // TODO: Fix the function signature. -fn sale_price(price: i64) -> { +fn sale_price(price: i64) -> i64 { if is_even(price) { price - 10 } else { diff --git a/rustlings/exercises/02_functions/functions5.rs b/rustlings/exercises/02_functions/functions5.rs index 34a2ac7..024010f 100644 --- a/rustlings/exercises/02_functions/functions5.rs +++ b/rustlings/exercises/02_functions/functions5.rs @@ -1,6 +1,5 @@ -// TODO: Fix the function body without changing the signature. fn square(num: i32) -> i32 { - num * num; + num * num } fn main() { diff --git a/rustlings/solutions/02_functions/functions1.rs b/rustlings/solutions/02_functions/functions1.rs index dcf2377..dc52744 100644 --- a/rustlings/solutions/02_functions/functions1.rs +++ b/rustlings/solutions/02_functions/functions1.rs @@ -1,4 +1,8 @@ +// Some function with the name `call_me` without arguments or a return value. +fn call_me() { + println!("Hello world!"); +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + call_me(); } diff --git a/rustlings/solutions/02_functions/functions2.rs b/rustlings/solutions/02_functions/functions2.rs index dcf2377..f14ffa3 100644 --- a/rustlings/solutions/02_functions/functions2.rs +++ b/rustlings/solutions/02_functions/functions2.rs @@ -1,4 +1,11 @@ +// The type of function arguments must be annotated. +// Added the type annotation `u64`. +fn call_me(num: u64) { + for i in 0..num { + println!("Ring! Call number {}", i + 1); + } +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + call_me(3); } diff --git a/rustlings/solutions/02_functions/functions3.rs b/rustlings/solutions/02_functions/functions3.rs index dcf2377..ce5fe8e 100644 --- a/rustlings/solutions/02_functions/functions3.rs +++ b/rustlings/solutions/02_functions/functions3.rs @@ -1,4 +1,10 @@ +fn call_me(num: u8) { + for i in 0..num { + println!("Ring! Call number {}", i + 1); + } +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // `call_me` expects an argument. + call_me(5); } diff --git a/rustlings/solutions/02_functions/functions4.rs b/rustlings/solutions/02_functions/functions4.rs index dcf2377..f823de2 100644 --- a/rustlings/solutions/02_functions/functions4.rs +++ b/rustlings/solutions/02_functions/functions4.rs @@ -1,4 +1,17 @@ +fn is_even(num: i64) -> bool { + num % 2 == 0 +} + +// The return type must always be annotated. +fn sale_price(price: i64) -> i64 { + if is_even(price) { + price - 10 + } else { + price - 3 + } +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + let original_price = 51; + println!("Your sale price is {}", sale_price(original_price)); } diff --git a/rustlings/solutions/02_functions/functions5.rs b/rustlings/solutions/02_functions/functions5.rs index dcf2377..677f327 100644 --- a/rustlings/solutions/02_functions/functions5.rs +++ b/rustlings/solutions/02_functions/functions5.rs @@ -1,4 +1,9 @@ +fn square(num: i32) -> i32 { + // Removed the semicolon `;` at the end of the line below to implicitly return the result. + num * num +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + let answer = square(3); + println!("The square of 3 is {answer}"); } -- cgit v1.2.3