mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Add solutions to functions
This commit is contained in:
parent
0f4c42d54e
commit
d0b843d6c4
|
@ -1,3 +1,5 @@
|
||||||
|
// TODO: Add some function with the name `call_me` without arguments or a return value.
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
call_me();
|
call_me(); // Don't change this line
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
fn main() {
|
// TODO: Add the missing type of the argument `num` after the colon `:`.
|
||||||
call_me(3);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call_me(num:) {
|
fn call_me(num:) {
|
||||||
for i in 0..num {
|
for i in 0..num {
|
||||||
println!("Ring! Call number {}", i + 1);
|
println!("Ring! Call number {}", i + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
call_me(3);
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
fn main() {
|
|
||||||
call_me();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call_me(num: u32) {
|
fn call_me(num: u32) {
|
||||||
for i in 0..num {
|
for i in 0..num {
|
||||||
println!("Ring! Call number {}", i + 1);
|
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
|
// 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
|
// Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.
|
||||||
// about the function bodies themselves, we're only interested in the signatures
|
// Don't worry about the function bodies themselves, we are only interested in
|
||||||
// for now. If anything, this is a good way to peek ahead to future exercises!)
|
// the signatures for now.
|
||||||
|
|
||||||
fn main() {
|
fn is_even(num: i64) -> bool {
|
||||||
let original_price = 51;
|
num % 2 == 0
|
||||||
println!("Your sale price is {}", sale_price(original_price));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sale_price(price: i32) -> {
|
// TODO: Fix the function signature.
|
||||||
|
fn sale_price(price: i64) -> {
|
||||||
if is_even(price) {
|
if is_even(price) {
|
||||||
price - 10
|
price - 10
|
||||||
} else {
|
} else {
|
||||||
|
@ -16,6 +16,7 @@ fn sale_price(price: i32) -> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_even(num: i32) -> bool {
|
fn main() {
|
||||||
num % 2 == 0
|
let original_price = 51;
|
||||||
|
println!("Your sale price is {}", sale_price(original_price));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
fn main() {
|
// TODO: Fix the function body without chaning the signature.
|
||||||
let answer = square(3);
|
|
||||||
println!("The square of 3 is {}", answer);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn square(num: i32) -> i32 {
|
fn square(num: i32) -> i32 {
|
||||||
num * num;
|
num * num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let answer = square(3);
|
||||||
|
println!("The square of 3 is {answer}");
|
||||||
|
}
|
||||||
|
|
|
@ -142,7 +142,7 @@ test = false
|
||||||
hint = """
|
hint = """
|
||||||
This `main` function is calling a function that it expects to exist, but the
|
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`.
|
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?"""
|
Sounds a lot like `main`, doesn't it?"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
@ -159,7 +159,7 @@ dir = "02_functions"
|
||||||
test = false
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
This time, the function *declaration* is okay, but there's something wrong
|
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]]
|
[[exercises]]
|
||||||
name = "functions4"
|
name = "functions4"
|
||||||
|
@ -167,8 +167,8 @@ dir = "02_functions"
|
||||||
test = false
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
The error message points to the function `sale_price` and says it expects a type
|
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
|
after `->`. This is where the function's return type should be.
|
||||||
look at the `is_even` function for an example!"""
|
Take a look at the `is_even` function for an example!"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "functions5"
|
name = "functions5"
|
||||||
|
@ -177,15 +177,15 @@ test = false
|
||||||
hint = """
|
hint = """
|
||||||
This is a really common error that can be fixed by removing one character.
|
This is a really common error that can be fixed by removing one character.
|
||||||
It happens because Rust distinguishes between expressions and statements:
|
It happens because Rust distinguishes between expressions and statements:
|
||||||
expressions return a value based on their operand(s), and statements simply
|
Expressions return a value based on their operand(s), and statements simply
|
||||||
return a `()` type which behaves just like `void` in C/C++ language.
|
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
|
We want to return a value with the type `i32` from the `square` function, but
|
||||||
returning a `()` type...
|
it is returning the type `()`.
|
||||||
|
|
||||||
They are not the same. There are two solutions:
|
There are two solutions:
|
||||||
1. Add a `return` ahead of `num * num;`
|
1. Add the `return` keyword before `num * num;`
|
||||||
2. remove `;`, make it to be `num * num`"""
|
2. Remove the semicolon `;` after `num * num`"""
|
||||||
|
|
||||||
# IF
|
# IF
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,6 @@ fn main() {
|
||||||
let mut x = 3;
|
let mut x = 3;
|
||||||
println!("Number {x}");
|
println!("Number {x}");
|
||||||
|
|
||||||
x = 5; // Don't change this line
|
x = 5;
|
||||||
println!("Number {x}");
|
println!("Number {x}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
fn main() {
|
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);
|
println!("Spell a number: {}", number);
|
||||||
|
|
||||||
// Using variable shadowing
|
// 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