mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-03 08:43:30 +02:00
Add solutions to functions
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user