mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-23 14:12:23 -05:00
Add solutions to functions
This commit is contained in:
parent
0f4c42d54e
commit
d0b843d6c4
13 changed files with 97 additions and 41 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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}");
|
||||
}
|
||||
|
|
|
@ -142,7 +142,7 @@ test = false
|
|||
hint = """
|
||||
This `main` function is calling a function that it expects to exist, but the
|
||||
function doesn't exist. It expects this function to have the name `call_me`.
|
||||
It expects this function to not take any arguments and not return a value.
|
||||
It also expects this function to not take any arguments and not return a value.
|
||||
Sounds a lot like `main`, doesn't it?"""
|
||||
|
||||
[[exercises]]
|
||||
|
@ -159,7 +159,7 @@ dir = "02_functions"
|
|||
test = false
|
||||
hint = """
|
||||
This time, the function *declaration* is okay, but there's something wrong
|
||||
with the place where we're calling the function."""
|
||||
with the place where we are calling the function."""
|
||||
|
||||
[[exercises]]
|
||||
name = "functions4"
|
||||
|
@ -167,8 +167,8 @@ dir = "02_functions"
|
|||
test = false
|
||||
hint = """
|
||||
The error message points to the function `sale_price` and says it expects a type
|
||||
after the `->`. This is where the function's return type should be -- take a
|
||||
look at the `is_even` function for an example!"""
|
||||
after `->`. This is where the function's return type should be.
|
||||
Take a look at the `is_even` function for an example!"""
|
||||
|
||||
[[exercises]]
|
||||
name = "functions5"
|
||||
|
@ -177,15 +177,15 @@ test = false
|
|||
hint = """
|
||||
This is a really common error that can be fixed by removing one character.
|
||||
It happens because Rust distinguishes between expressions and statements:
|
||||
expressions return a value based on their operand(s), and statements simply
|
||||
return a `()` type which behaves just like `void` in C/C++ language.
|
||||
Expressions return a value based on their operand(s), and statements simply
|
||||
return a `()` type which behaves just like `void` in C/C++.
|
||||
|
||||
We want to return a value of `i32` type from the `square` function, but it is
|
||||
returning a `()` type...
|
||||
We want to return a value with the type `i32` from the `square` function, but
|
||||
it is returning the type `()`.
|
||||
|
||||
They are not the same. There are two solutions:
|
||||
1. Add a `return` ahead of `num * num;`
|
||||
2. remove `;`, make it to be `num * num`"""
|
||||
There are two solutions:
|
||||
1. Add the `return` keyword before `num * num;`
|
||||
2. Remove the semicolon `;` after `num * num`"""
|
||||
|
||||
# IF
|
||||
|
||||
|
|
|
@ -4,6 +4,6 @@ fn main() {
|
|||
let mut x = 3;
|
||||
println!("Number {x}");
|
||||
|
||||
x = 5; // Don't change this line
|
||||
x = 5;
|
||||
println!("Number {x}");
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
fn main() {
|
||||
let number = "T-H-R-E-E"; // Don't change this line
|
||||
let number = "T-H-R-E-E";
|
||||
println!("Spell a number: {}", number);
|
||||
|
||||
// Using variable shadowing
|
||||
|
|
|
@ -1 +1,8 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
// Some function with the name `call_me` without arguments or a return value.
|
||||
fn call_me() {
|
||||
println!("Hello world!");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
call_me();
|
||||
}
|
||||
|
|
|
@ -1 +1,11 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
// 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() {
|
||||
call_me(3);
|
||||
}
|
||||
|
|
|
@ -1 +1,10 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
fn call_me(num: u32) {
|
||||
for i in 0..num {
|
||||
println!("Ring! Call number {}", i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// `call_me` expects an argument.
|
||||
call_me(5);
|
||||
}
|
||||
|
|
|
@ -1 +1,17 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
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() {
|
||||
let original_price = 51;
|
||||
println!("Your sale price is {}", sale_price(original_price));
|
||||
}
|
||||
|
|
|
@ -1 +1,9 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
fn square(num: i32) -> i32 {
|
||||
// Removed the semicolon `;` at the end of the line below to implicitely return the result.
|
||||
num * num
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let answer = square(3);
|
||||
println!("The square of 3 is {answer}");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue