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
15 changes: 11 additions & 4 deletions exercises/00_intro/intro1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// Try adding a new `println!` and check the updated output in the terminal.

fn main() {
println!(r#" Welcome to... "#);
println!("Hello and");
println!(r#" welcome to... "#);
println!(r#" _ _ _ "#);
println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#);
println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#);
Expand All @@ -18,7 +19,13 @@ fn main() {
println!("or logic error. The central concept behind Rustlings is to fix these errors and");
println!("solve the exercises. Good luck!");
println!();
println!("The file of this exercise is `exercises/00_intro/intro1.rs`. Have a look!");
println!("The current exercise path will be always shown under the progress bar.");
println!("You can click on the path to open the exercise file in your editor.");
println!("The source for this exercise is in `exercises/intro/intro1.rs`. Have a look!");
println!(
"Going forward, the source of the exercises will always be in the success/failure output."
);
println!();
println!(
"If you want to use rust-analyzer, Rust's LSP implementation, make sure your editor is set"
);
println!("up, and then run `rustlings lsp` before continuing.")
}
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 mut number:i32 = 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: 3 additions & 1 deletion exercises/02_functions/functions1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// TODO: Add some function with the name `call_me` without arguments or a return value.

fn call_me() {
// This function does nothing, but it must be defined.
}
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(3);
}
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
4 changes: 2 additions & 2 deletions exercises/02_functions/functions5.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// TODO: Fix the function body without changing the signature.
fn square(num: i32) -> i32 {
num * num;
num * num
}

fn main() {
let answer = square(3);
println!("The square of 3 is {answer}");
println!("The square of 3 is {}",answer);
}
9 changes: 9 additions & 0 deletions exercises/03_if/if1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ fn bigger(a: i32, b: i32) -> i32 {
// Do not use:
// - another function call
// - additional variables
if( a > b ) {
a
} else {
b
}
}

fn main() {
// You can optionally experiment here.
let a = 10;
let b = 20;
let result = bigger(a, b);
println!("The bigger number between {} and {} is {}", a, b, result);
}

// Don't mind this for now :)
Expand Down
7 changes: 6 additions & 1 deletion exercises/03_if/if2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else {
1
if food == "potato" {
"I guess I can eat that."
} else {
// This is the default case for foods that are not liked.
"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"
0
};

// 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 @@ -9,7 +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 …
if is_evening {
if !is_morning {
println!("Good evening!");
}
}
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 = '😊'; // Example character, you can change this
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 = [0; 100]; // Example array with 100 elements initialized to 0
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 @@ -7,7 +7,7 @@ mod tests {
#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];

let nice_slice = &a[1..4]; // Get a slice from index 1 to 3 (inclusive of 1, exclusive of 4)
// TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes.
// let nice_slice = ???

Expand Down
6 changes: 4 additions & 2 deletions exercises/04_primitive_types/primitive_types5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ fn main() {

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

println!("{name} is {age} years old");
let (name, age) = cat; // Destructuring the tuple into name and age
println!("{} is {} years old",
name,
age);
}
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; // Accessing the second element of the tuple
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]; // Vector
(a, v)
}

Expand Down
4 changes: 3 additions & 1 deletion 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 @@ -24,7 +25,8 @@ fn vec_map(input: &[i32]) -> Vec<i32> {
input
.iter()
.map(|element| {
// ???
// Multiply each element by 2
element * 2
})
.collect()
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/06_move_semantics/move_semantics1.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// TODO: Fix the compiler error in this function.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec;
let mut v = vec;

vec.push(88);
v.push(88);

vec
v
}

fn main() {
Expand Down
7 changes: 2 additions & 5 deletions exercises/06_move_semantics/move_semantics2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;

fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(88);

vec
}

Expand All @@ -20,7 +17,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);
}
16 changes: 12 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 @@ -20,7 +23,11 @@ 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 +37,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,8 +47,9 @@ 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!");
}
}
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
Loading