mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-05 10:19:18 +00:00
Complete functions exercises
This commit is contained in:
parent
69b6d2116b
commit
7cad97ec5b
@ -3,8 +3,14 @@
|
||||
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
println!("You define functions outside of main and call them here.");
|
||||
println!("Call me.");
|
||||
// Rust doesn't care where the function is defined, as long as it's within the scope.
|
||||
call_me();
|
||||
}
|
||||
|
||||
fn call_me() {
|
||||
println!("Beep me. If you wanna reach me.");
|
||||
}
|
||||
@ -3,13 +3,13 @@
|
||||
// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
call_me(3);
|
||||
}
|
||||
|
||||
fn call_me(num:) {
|
||||
fn call_me(num: i8) {
|
||||
// chose i8 because it's the smallest integer type
|
||||
for i in 0..num {
|
||||
println!("Ring! Call number {}", i + 1);
|
||||
}
|
||||
|
||||
@ -3,10 +3,8 @@
|
||||
// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
call_me();
|
||||
call_me(12);
|
||||
}
|
||||
|
||||
fn call_me(num: u32) {
|
||||
|
||||
@ -8,14 +8,13 @@
|
||||
// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
let original_price = 51;
|
||||
println!("Your sale price is {}", sale_price(original_price));
|
||||
}
|
||||
|
||||
fn sale_price(price: i32) -> {
|
||||
fn sale_price(price: i32) -> i32 {
|
||||
if is_even(price) {
|
||||
price - 10
|
||||
} else {
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
let answer = square(3);
|
||||
@ -11,5 +10,8 @@ fn main() {
|
||||
}
|
||||
|
||||
fn square(num: i32) -> i32 {
|
||||
num * num;
|
||||
// you can either add an explicit return to this expression
|
||||
// or remove the semicolon to make it an implicit return
|
||||
// you cannot just have an expression with a semicolon
|
||||
return num * num;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user