From 1f869d5cc85036b8d5be301160265901db5b1862 Mon Sep 17 00:00:00 2001 From: andwati Date: Mon, 7 Sep 2026 08:08:30 +0300 Subject: [PATCH] functions --- .rustlings-state.txt | 15 +++++++++++++-- exercises/01_variables/variables1.rs | 2 +- exercises/01_variables/variables2.rs | 2 +- exercises/01_variables/variables3.rs | 2 +- exercises/01_variables/variables4.rs | 2 +- exercises/01_variables/variables5.rs | 2 +- exercises/01_variables/variables6.rs | 2 +- exercises/02_functions/functions1.rs | 3 +++ exercises/02_functions/functions2.rs | 2 +- exercises/02_functions/functions3.rs | 2 +- exercises/02_functions/functions4.rs | 2 +- exercises/02_functions/functions5.rs | 2 +- solutions/01_variables/variables1.rs | 6 ++++-- solutions/02_functions/functions1.rs | 8 ++++++-- solutions/02_functions/functions2.rs | 11 +++++++++-- solutions/02_functions/functions3.rs | 10 ++++++++-- solutions/02_functions/functions4.rs | 17 +++++++++++++++-- solutions/02_functions/functions5.rs | 9 +++++++-- 18 files changed, 75 insertions(+), 24 deletions(-) diff --git a/.rustlings-state.txt b/.rustlings-state.txt index 13f1027..ce94351 100644 --- a/.rustlings-state.txt +++ b/.rustlings-state.txt @@ -1,6 +1,17 @@ DON'T EDIT THIS FILE! -variables1 +if1 intro1 -intro2 \ No newline at end of file +intro2 +variables1 +variables2 +variables3 +variables4 +variables5 +variables6 +functions1 +functions2 +functions3 +functions4 +functions5 \ No newline at end of file diff --git a/exercises/01_variables/variables1.rs b/exercises/01_variables/variables1.rs index f83b44d..ec1bcac 100644 --- a/exercises/01_variables/variables1.rs +++ b/exercises/01_variables/variables1.rs @@ -1,6 +1,6 @@ fn main() { // TODO: Add the missing keyword. - x = 5; + let x = 5; println!("x has the value {x}"); } diff --git a/exercises/01_variables/variables2.rs b/exercises/01_variables/variables2.rs index e2a3603..e2fd5d0 100644 --- a/exercises/01_variables/variables2.rs +++ b/exercises/01_variables/variables2.rs @@ -1,6 +1,6 @@ fn main() { // TODO: Change the line below to fix the compiler error. - let x; + let x = 10; if x == 10 { println!("x is ten!"); diff --git a/exercises/01_variables/variables3.rs b/exercises/01_variables/variables3.rs index 06f35bb..45a8586 100644 --- a/exercises/01_variables/variables3.rs +++ b/exercises/01_variables/variables3.rs @@ -1,6 +1,6 @@ fn main() { // TODO: Change the line below to fix the compiler error. - let x: i32; + let x: i32 = 10; println!("Number {x}"); } diff --git a/exercises/01_variables/variables4.rs b/exercises/01_variables/variables4.rs index 6c138b1..90d4ef0 100644 --- a/exercises/01_variables/variables4.rs +++ b/exercises/01_variables/variables4.rs @@ -1,6 +1,6 @@ // TODO: Fix the compiler error. fn main() { - let x = 3; + let mut x = 3; println!("Number {x}"); x = 5; // Don't change this line diff --git a/exercises/01_variables/variables5.rs b/exercises/01_variables/variables5.rs index cf5620d..085e099 100644 --- a/exercises/01_variables/variables5.rs +++ b/exercises/01_variables/variables5.rs @@ -3,6 +3,6 @@ fn main() { println!("Spell a number: {number}"); // TODO: Fix the compiler error by changing the line below without renaming the variable. - number = 3; + let number = 3; println!("Number plus two is: {}", number + 2); } diff --git a/exercises/01_variables/variables6.rs b/exercises/01_variables/variables6.rs index 4a040fd..deb33ed 100644 --- a/exercises/01_variables/variables6.rs +++ b/exercises/01_variables/variables6.rs @@ -1,5 +1,5 @@ // TODO: Change the line below to fix the compiler error. -const NUMBER = 3; +const NUMBER: i32 = 3; fn main() { println!("Number: {NUMBER}"); diff --git a/exercises/02_functions/functions1.rs b/exercises/02_functions/functions1.rs index a812c21..846d027 100644 --- a/exercises/02_functions/functions1.rs +++ b/exercises/02_functions/functions1.rs @@ -1,5 +1,8 @@ // TODO: Add some function with the name `call_me` without arguments or a return value. +fn call_me() { + println!("I was called!"); +} fn main() { call_me(); // Don't change this line } diff --git a/exercises/02_functions/functions2.rs b/exercises/02_functions/functions2.rs index 2c773c6..a2cffc1 100644 --- a/exercises/02_functions/functions2.rs +++ b/exercises/02_functions/functions2.rs @@ -1,5 +1,5 @@ // TODO: Add the missing type of the argument `num` after the colon `:`. -fn call_me(num:) { +fn call_me(num: i32) { for i in 0..num { println!("Ring! Call number {}", i + 1); } diff --git a/exercises/02_functions/functions3.rs b/exercises/02_functions/functions3.rs index 8d65477..a226e66 100644 --- a/exercises/02_functions/functions3.rs +++ b/exercises/02_functions/functions3.rs @@ -6,5 +6,5 @@ fn call_me(num: u8) { fn main() { // TODO: Fix the function call. - call_me(); + call_me(1); } diff --git a/exercises/02_functions/functions4.rs b/exercises/02_functions/functions4.rs index b22bffd..c1e078c 100644 --- a/exercises/02_functions/functions4.rs +++ b/exercises/02_functions/functions4.rs @@ -8,7 +8,7 @@ fn is_even(num: i64) -> bool { } // 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/exercises/02_functions/functions5.rs b/exercises/02_functions/functions5.rs index 34a2ac7..a063100 100644 --- a/exercises/02_functions/functions5.rs +++ b/exercises/02_functions/functions5.rs @@ -1,6 +1,6 @@ // TODO: Fix the function body without changing the signature. fn square(num: i32) -> i32 { - num * num; + num * num } fn main() { diff --git a/solutions/01_variables/variables1.rs b/solutions/01_variables/variables1.rs index dcf2377..58d046b 100644 --- a/solutions/01_variables/variables1.rs +++ b/solutions/01_variables/variables1.rs @@ -1,4 +1,6 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // Declaring variables requires the `let` keyword. + let x = 5; + + println!("x has the value {x}"); } diff --git a/solutions/02_functions/functions1.rs b/solutions/02_functions/functions1.rs index dcf2377..dc52744 100644 --- a/solutions/02_functions/functions1.rs +++ b/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/solutions/02_functions/functions2.rs b/solutions/02_functions/functions2.rs index dcf2377..f14ffa3 100644 --- a/solutions/02_functions/functions2.rs +++ b/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/solutions/02_functions/functions3.rs b/solutions/02_functions/functions3.rs index dcf2377..ce5fe8e 100644 --- a/solutions/02_functions/functions3.rs +++ b/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/solutions/02_functions/functions4.rs b/solutions/02_functions/functions4.rs index dcf2377..f823de2 100644 --- a/solutions/02_functions/functions4.rs +++ b/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/solutions/02_functions/functions5.rs b/solutions/02_functions/functions5.rs index dcf2377..677f327 100644 --- a/solutions/02_functions/functions5.rs +++ b/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}"); }