1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-11-29 22:47:43 +02:00

Add solutions to functions

This commit is contained in:
mo8it
2024-05-21 02:43:18 +02:00
parent 0f4c42d54e
commit d0b843d6c4
13 changed files with 97 additions and 41 deletions

View File

@@ -1,3 +1,5 @@
// TODO: Add some function with the name `call_me` without arguments or a return value.
fn main() {
call_me();
call_me(); // Don't change this line
}

View File

@@ -1,9 +1,10 @@
fn main() {
call_me(3);
}
// TODO: Add the missing type of the argument `num` after the colon `:`.
fn call_me(num:) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
fn main() {
call_me(3);
}

View File

@@ -1,9 +1,10 @@
fn main() {
call_me();
}
fn call_me(num: u32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
fn main() {
// TODO: Fix the function call.
call_me();
}

View File

@@ -1,14 +1,14 @@
// 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're only interested in the signatures
// for now. If anything, this is a good way to peek ahead to future exercises!)
// 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 main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
fn is_even(num: i64) -> bool {
num % 2 == 0
}
fn sale_price(price: i32) -> {
// TODO: Fix the function signature.
fn sale_price(price: i64) -> {
if is_even(price) {
price - 10
} else {
@@ -16,6 +16,7 @@ fn sale_price(price: i32) -> {
}
}
fn is_even(num: i32) -> bool {
num % 2 == 0
fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}

View File

@@ -1,8 +1,9 @@
fn main() {
let answer = square(3);
println!("The square of 3 is {}", answer);
}
// TODO: Fix the function body without chaning the signature.
fn square(num: i32) -> i32 {
num * num;
}
fn main() {
let answer = square(3);
println!("The square of 3 is {answer}");
}