mirror of
https://github.com/andwati/rustlings.git
synced 2026-10-18 04:02:52 +00:00
functions
This commit is contained in:
+13
-2
@@ -1,6 +1,17 @@
|
|||||||
DON'T EDIT THIS FILE!
|
DON'T EDIT THIS FILE!
|
||||||
|
|
||||||
variables1
|
if1
|
||||||
|
|
||||||
intro1
|
intro1
|
||||||
intro2
|
intro2
|
||||||
|
variables1
|
||||||
|
variables2
|
||||||
|
variables3
|
||||||
|
variables4
|
||||||
|
variables5
|
||||||
|
variables6
|
||||||
|
functions1
|
||||||
|
functions2
|
||||||
|
functions3
|
||||||
|
functions4
|
||||||
|
functions5
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
// TODO: Add the missing keyword.
|
// TODO: Add the missing keyword.
|
||||||
x = 5;
|
let x = 5;
|
||||||
|
|
||||||
println!("x has the value {x}");
|
println!("x has the value {x}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
// TODO: Change the line below to fix the compiler error.
|
// TODO: Change the line below to fix the compiler error.
|
||||||
let x;
|
let x = 10;
|
||||||
|
|
||||||
if x == 10 {
|
if x == 10 {
|
||||||
println!("x is ten!");
|
println!("x is ten!");
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
// TODO: Change the line below to fix the compiler error.
|
// TODO: Change the line below to fix the compiler error.
|
||||||
let x: i32;
|
let x: i32 = 10;
|
||||||
|
|
||||||
println!("Number {x}");
|
println!("Number {x}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// TODO: Fix the compiler error.
|
// TODO: Fix the compiler error.
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = 3;
|
let mut x = 3;
|
||||||
println!("Number {x}");
|
println!("Number {x}");
|
||||||
|
|
||||||
x = 5; // Don't change this line
|
x = 5; // Don't change this line
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ fn main() {
|
|||||||
println!("Spell a number: {number}");
|
println!("Spell a number: {number}");
|
||||||
|
|
||||||
// TODO: Fix the compiler error by changing the line below without renaming the variable.
|
// 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);
|
println!("Number plus two is: {}", number + 2);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// TODO: Change the line below to fix the compiler error.
|
// TODO: Change the line below to fix the compiler error.
|
||||||
const NUMBER = 3;
|
const NUMBER: i32 = 3;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Number: {NUMBER}");
|
println!("Number: {NUMBER}");
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
// TODO: Add some function with the name `call_me` without arguments or a return value.
|
// TODO: Add some function with the name `call_me` without arguments or a return value.
|
||||||
|
|
||||||
|
fn call_me() {
|
||||||
|
println!("I was called!");
|
||||||
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
call_me(); // Don't change this line
|
call_me(); // Don't change this line
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// TODO: Add the missing type of the argument `num` after the colon `:`.
|
// 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 {
|
for i in 0..num {
|
||||||
println!("Ring! Call number {}", i + 1);
|
println!("Ring! Call number {}", i + 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ fn call_me(num: u8) {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// TODO: Fix the function call.
|
// TODO: Fix the function call.
|
||||||
call_me();
|
call_me(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ fn is_even(num: i64) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Fix the function signature.
|
// TODO: Fix the function signature.
|
||||||
fn sale_price(price: i64) -> {
|
fn sale_price(price: i64) -> i64 {
|
||||||
if is_even(price) {
|
if is_even(price) {
|
||||||
price - 10
|
price - 10
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// TODO: Fix the function body without changing the signature.
|
// TODO: Fix the function body without changing the signature.
|
||||||
fn square(num: i32) -> i32 {
|
fn square(num: i32) -> i32 {
|
||||||
num * num;
|
num * num
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
// Declaring variables requires the `let` keyword.
|
||||||
// It will be automatically filled after you finish the exercise.
|
let x = 5;
|
||||||
|
|
||||||
|
println!("x has the value {x}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
call_me();
|
||||||
// It will be automatically filled after you finish the exercise.
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
call_me(3);
|
||||||
// It will be automatically filled after you finish the exercise.
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
|
fn call_me(num: u8) {
|
||||||
|
for i in 0..num {
|
||||||
|
println!("Ring! Call number {}", i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
// `call_me` expects an argument.
|
||||||
// It will be automatically filled after you finish the exercise.
|
call_me(5);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
let original_price = 51;
|
||||||
// It will be automatically filled after you finish the exercise.
|
println!("Your sale price is {}", sale_price(original_price));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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() {
|
fn main() {
|
||||||
// DON'T EDIT THIS SOLUTION FILE!
|
let answer = square(3);
|
||||||
// It will be automatically filled after you finish the exercise.
|
println!("The square of 3 is {answer}");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user