first go at the exercises
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
fn main() {
|
||||
// TODO: Fix the code to print "Hello world!".
|
||||
printline!("Hello world!");
|
||||
println!("Hello world!");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
// TODO: Add the missing keyword.
|
||||
x = 5;
|
||||
let x = 5;
|
||||
|
||||
println!("x has the value {x}");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
// TODO: Change the line below to fix the compiler error.
|
||||
let x;
|
||||
let x = 10;
|
||||
|
||||
if x == 10 {
|
||||
println!("x is ten!");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
// TODO: Change the line below to fix the compiler error.
|
||||
let x: i32;
|
||||
let x: i32 = 5;
|
||||
|
||||
println!("Number {x}");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// TODO: Fix the compiler error.
|
||||
fn main() {
|
||||
let x = 3;
|
||||
let mut x = 3;
|
||||
println!("Number {x}");
|
||||
|
||||
x = 5; // Don't change this line
|
||||
|
||||
@@ -3,6 +3,6 @@ fn main() {
|
||||
println!("Spell a number: {}", number);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// TODO: Change the line below to fix the compiler error.
|
||||
const NUMBER = 3;
|
||||
const NUMBER: u32 = 3;
|
||||
|
||||
fn main() {
|
||||
println!("Number: {NUMBER}");
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
// TODO: Add some function with the name `call_me` without arguments or a return value.
|
||||
|
||||
fn call_me()
|
||||
{
|
||||
println!("hoi");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
call_me(); // Don't change this line
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// TODO: Add the missing type of the argument `num` after the colon `:`.
|
||||
fn call_me(num:) {
|
||||
fn call_me(num: u32) {
|
||||
for i in 0..num {
|
||||
println!("Ring! Call number {}", i + 1);
|
||||
}
|
||||
|
||||
@@ -6,5 +6,5 @@ fn call_me(num: u8) {
|
||||
|
||||
fn main() {
|
||||
// TODO: Fix the function call.
|
||||
call_me();
|
||||
call_me(255);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ fn is_even(num: i64) -> bool {
|
||||
}
|
||||
|
||||
// TODO: Fix the function signature.
|
||||
fn sale_price(price: i64) -> {
|
||||
fn sale_price(price: i64) -> i64{
|
||||
if is_even(price) {
|
||||
price - 10
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// TODO: Fix the function body without changing the signature.
|
||||
fn square(num: i32) -> i32 {
|
||||
num * num;
|
||||
num * num
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -4,6 +4,12 @@ fn bigger(a: i32, b: i32) -> i32 {
|
||||
// Do not use:
|
||||
// - another function call
|
||||
// - additional variables
|
||||
if a < b {
|
||||
b
|
||||
}
|
||||
else {
|
||||
a
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
fn foo_if_fizz(fizzish: &str) -> &str {
|
||||
if fizzish == "fizz" {
|
||||
"foo"
|
||||
} else {
|
||||
1
|
||||
}
|
||||
else if fizzish == "fuzz" {
|
||||
"bar"
|
||||
}
|
||||
else {
|
||||
"baz"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ fn animal_habitat(animal: &str) -> &str {
|
||||
let identifier = if animal == "crab" {
|
||||
1
|
||||
} else if animal == "gopher" {
|
||||
2.0
|
||||
2
|
||||
} else if animal == "snake" {
|
||||
3
|
||||
} else {
|
||||
"Unknown"
|
||||
4
|
||||
};
|
||||
|
||||
// Don't change the expression below!
|
||||
|
||||
@@ -9,6 +9,7 @@ fn main() {
|
||||
// TODO: Define a boolean variable with the name `is_evening` before the `if` statement below.
|
||||
// The value of the variable should be the negation (opposite) of `is_morning`.
|
||||
// let …
|
||||
let is_evening = !is_morning;
|
||||
if is_evening {
|
||||
println!("Good evening!");
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ fn main() {
|
||||
// Try a letter, try a digit (in single quotes), try a special character, try a character
|
||||
// from a different language than your own, try an emoji 😉
|
||||
// let your_character = '';
|
||||
let your_character = 'e';
|
||||
|
||||
if your_character.is_alphabetic() {
|
||||
println!("Alphabetical!");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
fn main() {
|
||||
// TODO: Create an array called `a` with at least 100 elements in it.
|
||||
// let a = ???
|
||||
let a = (1..150).collect::<std::vec::Vec<u8>>();
|
||||
|
||||
if a.len() >= 100 {
|
||||
println!("Wow, that's a big array!");
|
||||
|
||||
@@ -10,6 +10,7 @@ mod tests {
|
||||
|
||||
// TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes.
|
||||
// let nice_slice = ???
|
||||
let nice_slice = &a[1..4];
|
||||
|
||||
assert_eq!([2, 3, 4], nice_slice);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ fn main() {
|
||||
|
||||
// TODO: Destructure the `cat` tuple in one statement so that the println works.
|
||||
// let /* your pattern here */ = cat;
|
||||
let name = cat.0;
|
||||
let age = cat.1;
|
||||
|
||||
println!("{name} is {age} years old");
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ mod tests {
|
||||
// TODO: Use a tuple index to access the second element of `numbers`
|
||||
// and assign it to a variable called `second`.
|
||||
// let second = ???;
|
||||
let second = numbers.1;
|
||||
|
||||
assert_eq!(second, 2, "This is not the 2nd number in the tuple!");
|
||||
}
|
||||
|
||||
@@ -11,6 +11,13 @@
|
||||
// TODO: Write a function that calculates the price of an order of apples given
|
||||
// the quantity bought.
|
||||
// fn calculate_price_of_apples(???) -> ??? { ??? }
|
||||
fn calculate_price_of_apples(apples: u16) -> u16 {
|
||||
if apples <= 40 {
|
||||
apples * 2
|
||||
} else {
|
||||
apples
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
|
||||
Reference in New Issue
Block a user