Skip to content
Closed

Solving #2448

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
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mirrord.promptUsingBinary": false,
"makefile.configureOnOpen": false
}
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
format:
cargo fmt --all --verbose --check
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/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 = 10;

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 = 0;

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
2 changes: 1 addition & 1 deletion exercises/01_variables/variables5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
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: i8 = 3;

fn main() {
println!("Number: {NUMBER}");
Expand Down
3 changes: 3 additions & 0 deletions exercises/02_functions/functions1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// TODO: Add some function with the name `call_me` without arguments or a return value.


fn call_me() {}

fn main() {
call_me(); // Don't change this line
}
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
7 changes: 4 additions & 3 deletions exercises/03_if/if1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
fn bigger(a: i32, b: i32) -> i32 {
// TODO: Complete this function to return the bigger number!
// If both numbers are equal, any of them can be returned.
// Do not use:
// - another function call
// - additional variables
if a > b {
return a
}
b
}

fn main() {
Expand Down
7 changes: 5 additions & 2 deletions exercises/03_if/if2.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// TODO: Fix the compiler error on this function.
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
return "Yummy!"
}
if food == "potato" {
"I guess I can eat that."
} else {
1
"No thanks!"
}
}

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
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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!");
}
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 @@ -16,7 +16,7 @@ fn main() {
// below with your favorite character.
// 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 = 'X';

if your_character.is_alphabetic() {
println!("Alphabetical!");
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,6 +1,6 @@
fn main() {
// TODO: Create an array called `a` with at least 100 elements in it.
// let a = ???
let a = [0; 100];

if a.len() >= 100 {
println!("Wow, that's a big array!");
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 @@ -9,7 +9,7 @@ mod tests {
let a = [1, 2, 3, 4, 5];

// 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);
}
Expand Down
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 @@ -2,7 +2,7 @@ fn main() {
let cat = ("Furry McFurson", 3.5);

// 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 @@ -10,7 +10,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!");
}
Expand Down
1 change: 1 addition & 0 deletions exercises/05_vecs/vecs1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fn elems_to_vec(a: i32, b: i32, c: i32) -> Vec<i32> {
// TODO: Return a vector containing the elements a, b and c (in this order).
// Use the "vec!" macro.
vec![a,b,c]
}

fn main() {
Expand Down
1 change: 1 addition & 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 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);
}
11 changes: 7 additions & 4 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: u8,
green: u8,
blue: u8
}

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

#[derive(Debug)]
struct UnitStruct;
Expand All @@ -19,7 +22,7 @@ mod tests {
#[test]
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);
Expand All @@ -29,7 +32,7 @@ mod tests {
#[test]
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);
Expand All @@ -39,7 +42,7 @@ mod tests {
#[test]
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 @@ -34,7 +34,15 @@ mod tests {
let order_template = create_order_template();

// 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: 2019,
made_by_phone: false,
made_by_mobile: false,
made_by_email: true,
item_number: 123,
count: 1,
};

assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
Expand Down
29 changes: 18 additions & 11 deletions exercises/07_structs/structs3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ struct Fireworks {
rockets: usize,
}

// TODO: Turn this function into an associated function on `Fireworks`.
fn new_fireworks() -> Fireworks {
Fireworks { rockets: 0 }
}
impl Fireworks {
fn new() -> Self {
Self::new_fireworks()
}

// TODO: Turn this function into a method on `Fireworks`.
fn add_rockets(fireworks: &mut Fireworks, rockets: usize) {
fireworks.rockets += rockets
}
// TODO: Turn this function into an associated function on `Fireworks`.
fn new_fireworks() -> Self {
Self { rockets: 0 }
}

// TODO: Turn this function into a method on `Fireworks`.
fn start(fireworks: Fireworks) -> String {
"🚀".repeat(fireworks.rockets)
// TODO: Turn this function into a method on `Fireworks`.
fn add_rockets(&mut self, rockets: usize) {
self.rockets += rockets
}

// TODO: Turn this function into a method on `Fireworks`.
fn start(self) -> String {
"🚀".repeat(self.rockets)
}
}


fn main() {
// You can optionally experiment here.
}
Expand Down
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
5 changes: 5 additions & 0 deletions exercises/08_enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ struct Point {
#[derive(Debug)]
enum Message {
// TODO: Define the different variants used below.
Resize { width: u8, height: u8 },
Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
Quit
}

impl Message {
Expand Down
Loading
Loading