Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/00_intro/intro2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
// TODO: Fix the code to print "Hello world!".
printline!("Hello world!");
println!("Hello world!");
}
2 changes: 1 addition & 1 deletion exercises/01_variables/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Variables

In Rust, variables are immutable by default.
When a variable is immutable, once a value is bound to a name, you can't change that value.
When a variable is immutable, once a value is bound to a name, you cant change that value.
You can make them mutable by adding `mut` in front of the variable name.

## Further information
Expand Down
2 changes: 1 addition & 1 deletion exercises/01_variables/variables1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
// TODO: Add the missing keyword.
x = 5;
let x = 5;

println!("x has the value {x}");
}
2 changes: 1 addition & 1 deletion exercises/01_variables/variables2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
// TODO: Change the line below to fix the compiler error.
let x;
let x = 1;

if x == 10 {
println!("x is ten!");
Expand Down
2 changes: 1 addition & 1 deletion exercises/01_variables/variables3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
// TODO: Change the line below to fix the compiler error.
let x: i32;
let x: i32 = 1;

println!("Number {x}");
}
2 changes: 1 addition & 1 deletion exercises/01_variables/variables4.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions exercises/01_variables/variables5.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fn main() {
let number = "T-H-R-E-E"; // Don't change this line
println!("Spell a number: {number}");
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);
}
2 changes: 1 addition & 1 deletion exercises/01_variables/variables6.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: Change the line below to fix the compiler error.
const NUMBER = 3;
const NUMBER : i32 = 3;

fn main() {
println!("Number: {NUMBER}");
Expand Down
4 changes: 4 additions & 0 deletions exercises/02_functions/functions1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
fn main() {
call_me(); // Don't change this line
}

fn call_me() {

}
2 changes: 1 addition & 1 deletion exercises/02_functions/functions2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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 {
println!("Ring! Call number {}", i + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_functions/functions3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ fn call_me(num: u8) {

fn main() {
// TODO: Fix the function call.
call_me();
call_me(1);
}
2 changes: 1 addition & 1 deletion exercises/02_functions/functions4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_functions/functions5.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO: Fix the function body without changing the signature.
fn square(num: i32) -> i32 {
num * num;
num * num
}

fn main() {
Expand Down
5 changes: 5 additions & 0 deletions exercises/03_if/if1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ fn bigger(a: i32, b: i32) -> i32 {
// Do not use:
// - another function call
// - additional variables
if a > b {
a
} else {
b
}
}

fn main() {
Expand Down
6 changes: 4 additions & 2 deletions exercises/03_if/if2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else if food == "potato" {
"I guess I can eat that."
} else {
1
"No thanks!"
}
}

Expand All @@ -19,7 +21,7 @@ mod tests {

#[test]
fn yummy_food() {
// This means that calling `picky_eater` with the argument "strawberry" should return "Yummy!".
// This means that calling `picky_eater` with the argument "food" should return "Yummy!".
assert_eq!(picky_eater("strawberry"), "Yummy!");
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/03_if/if3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
1 change: 1 addition & 0 deletions exercises/04_primitive_types/primitive_types1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = false;
if is_evening {
println!("Good evening!");
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +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 = '1';
if your_character.is_alphabetic() {
println!("Alphabetical!");
} else if your_character.is_numeric() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() {
// TODO: Create an array called `a` with at least 100 elements in it.
// let a = ???

let a : [i32; 1000] = [0; 1000];
if a.len() >= 100 {
println!("Wow, that's a big array!");
} else {
Expand Down
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +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);
}
}
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ fn main() {

// TODO: Destructure the `cat` tuple in one statement so that the println works.
// let /* your pattern here */ = cat;

let (name, age) = cat;
println!("{name} is {age} years old");
}
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +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!");
}
}
2 changes: 1 addition & 1 deletion exercises/05_vecs/vecs1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn array_and_vec() -> ([i32; 4], Vec<i32>) {
// TODO: Create a vector called `v` which contains the exact same elements as in the array `a`.
// Use the vector macro.
// let v = ???;

let v = vec![10, 20, 30, 40];
(a, v)
}

Expand Down
2 changes: 2 additions & 0 deletions exercises/05_vecs/vecs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ fn vec_loop(input: &[i32]) -> Vec<i32> {
for element in input {
// TODO: Multiply each element in the `input` slice by 2 and push it to
// the `output` vector.
output.push(*element * 2);
}

output
Expand All @@ -25,6 +26,7 @@ fn vec_map(input: &[i32]) -> Vec<i32> {
.iter()
.map(|element| {
// ???
element * 2
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/06_move_semantics/move_semantics1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO: Fix the compiler error in this function.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec;
let mut vec = vec;

vec.push(88);

Expand Down
2 changes: 1 addition & 1 deletion exercises/06_move_semantics/move_semantics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod tests {
fn move_semantics2() {
let vec0 = vec![22, 44, 66];

let vec1 = fill_vec(vec0);
let vec1 = fill_vec(vec0.clone());

assert_eq!(vec0, [22, 44, 66]);
assert_eq!(vec1, [22, 44, 66, 88]);
Expand Down
2 changes: 1 addition & 1 deletion exercises/06_move_semantics/move_semantics3.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: Fix the compiler error in the function without adding any new line.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(88);

vec
Expand Down
2 changes: 1 addition & 1 deletion exercises/06_move_semantics/move_semantics4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod tests {
fn move_semantics4() {
let mut x = Vec::new();
let y = &mut x;
let z = &mut x;
y.push(42);
let z = &mut x;
z.push(13);
assert_eq!(x, [42, 13]);
}
Expand Down
8 changes: 4 additions & 4 deletions exercises/06_move_semantics/move_semantics5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
// removing references (the character `&`).

// Shouldn't take ownership
fn get_char(data: String) -> char {
fn get_char(data: &String) -> char {
data.chars().last().unwrap()
}

// Should take ownership
fn string_uppercase(mut data: &String) {
fn string_uppercase(mut data: String) {
data = data.to_uppercase();

println!("{data}");
Expand All @@ -18,7 +18,7 @@ fn string_uppercase(mut data: &String) {
fn main() {
let data = "Rust is great!".to_string();

get_char(data);
get_char(&data);

string_uppercase(&data);
string_uppercase(data);
}
10 changes: 7 additions & 3 deletions exercises/07_structs/structs1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
struct ColorRegularStruct {
// TODO: Add the fields that the test `regular_structs` expects.
// What types should the fields have? What are the minimum and maximum values for RGB colors?
red:i32,
green:i32,
blue:i32,
}

struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */i32,i32,i32);

#[derive(Debug)]
struct UnitStruct;
Expand All @@ -20,7 +23,7 @@ mod tests {
fn regular_structs() {
// TODO: Instantiate a regular struct.
// let green =

let green=ColorRegularStruct{red:0,green:255,blue:0};
assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
assert_eq!(green.blue, 0);
Expand All @@ -30,7 +33,7 @@ mod tests {
fn tuple_structs() {
// TODO: Instantiate a tuple struct.
// let green =

let green=ColorTupleStruct(0,255,0);
assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
assert_eq!(green.2, 0);
Expand All @@ -40,6 +43,7 @@ mod tests {
fn unit_structs() {
// TODO: Instantiate a unit struct.
// let unit_struct =
let unit_struct = UnitStruct;
let message = format!("{unit_struct:?}s are fun!");

assert_eq!(message, "UnitStructs are fun!");
Expand Down
10 changes: 9 additions & 1 deletion exercises/07_structs/structs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ mod tests {

// TODO: Create your own order using the update syntax and template above!
// let your_order =

let your_order = Order {
name : String::from("Hacker in Rust"),
year : order_template.year,
made_by_phone : order_template.made_by_phone,
made_by_mobile : order_template.made_by_mobile,
made_by_email : order_template.made_by_email,
item_number : order_template.item_number,
count : 1,
};
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
Expand Down
6 changes: 4 additions & 2 deletions exercises/07_structs/structs3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ impl Package {
}

// TODO: Add the correct return type to the function signature.
fn is_international(&self) {
fn is_international(&self) ->bool {
// TODO: Read the tests that use this method to find out when a package
// is considered international.
self.sender_country != self.recipient_country
}

// TODO: Add the correct return type to the function signature.
fn get_fees(&self, cents_per_gram: u32) {
fn get_fees(&self, cents_per_gram: u32) -> u32 {
// TODO: Calculate the package's fees.
cents_per_gram * self.weight_in_grams
}
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/08_enums/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Enums

Rust allows you to define types called "enums" which enumerate possible values.
Enums are a feature in many languages, but their capabilities differ in each language. Rust's enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell.
Enums are a feature in many languages, but their capabilities differ in each language. Rusts enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell.
Useful in combination with enums is Rust's "pattern matching" facility, which makes it easy to run different code for different values of an enumeration.

## Further information

- [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html)
- [Pattern syntax](https://doc.rust-lang.org/book/ch19-03-pattern-syntax.html)
- [Pattern syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html)
5 changes: 5 additions & 0 deletions exercises/08_enums/enums1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#[derive(Debug)]
enum Message {
// TODO: Define a few types of messages as used below.
Resize,
Move,
Echo,
ChangeColor,
Quit,
}

fn main() {
Expand Down
Loading