Skip to content
Closed
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
5 changes: 4 additions & 1 deletion exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
//
// No hints this time! ;)

// I AM NOT DONE

#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor,
}

fn main() {
Expand Down
9 changes: 7 additions & 2 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Move {
x: u8,
y: u8,
},
Echo (String),
ChangeColor (u8,u8,u8),
Quit,
}

impl Message {
Expand Down
11 changes: 11 additions & 0 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

enum Message {
// TODO: implement the message variant types based on their usage below
Move (Point),
Echo (String),
ChangeColor (u8,u8,u8),
Quit,
}

struct Point {
Expand Down Expand Up @@ -45,6 +49,13 @@ impl State {
// variants
// Remember: When passing a tuple as a function argument, you'll need
// extra parentheses: fn function((t, u, p, l, e))
match message {
Message::Move (point) => self.move_position (point),
Message::ChangeColor (x,y,z) => self.change_color((x,y,z)),
Message::Echo (message) => self.echo (message),
Message::Quit => self.quit(),
}

}
}

Expand Down
6 changes: 5 additions & 1 deletion exercises/functions/functions1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn call_me(){

}

fn main() {
call_me();
Expand Down
4 changes: 2 additions & 2 deletions exercises/functions/functions2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {
call_me(3);
}

fn call_me(num:) {
fn call_me(num:i8) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
Expand Down
5 changes: 3 additions & 2 deletions exercises/functions/functions3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {
call_me();
call_me(5);
}

fn call_me(num: u32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}

}
8 changes: 4 additions & 4 deletions exercises/functions/functions4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {
let original_price = 51;
let original_price = 10;
println!("Your sale price is {}", sale_price(original_price));
}

fn sale_price(price: i32) -> {
fn sale_price(price: u32) -> u32{
if is_even(price) {
price - 10
} else {
price - 3
}
}

fn is_even(num: i32) -> bool {
fn is_even(num: u32) -> bool {
num % 2 == 0
}
4 changes: 2 additions & 2 deletions exercises/functions/functions5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {
let answer = square(3);
println!("The square of 3 is {}", answer);
}

fn square(num: i32) -> i32 {
num * num;
num * num
}
5 changes: 4 additions & 1 deletion exercises/if/if1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
//
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE


pub fn bigger(a: i32, b: i32) -> i32 {
// Complete this function to return the bigger number!
// Do not use:
// - another function call
// - additional variables
if a > b { return a; } b
}

// Don't mind this for now :)
Expand All @@ -31,3 +32,5 @@ mod tests {
assert_eq!(42, bigger(42, 42));
}
}


6 changes: 4 additions & 2 deletions exercises/if/if2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
//
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE


pub fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else if fizzish == "fuzz" {
"bar"
} else {
1
"baz"
}
}

Expand Down
5 changes: 2 additions & 3 deletions exercises/if/if3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
//
// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

pub fn animal_habitat(animal: &str) -> &'static str {
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
2.0
2
} else if animal == "snake" {
3
} else {
"Unknown"
4
};

// DO NOT CHANGE THIS STATEMENT BELOW
Expand Down
2 changes: 1 addition & 1 deletion exercises/intro/intro1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Execute `rustlings hint intro1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {
println!("Hello and");
Expand Down
4 changes: 2 additions & 2 deletions exercises/intro/intro2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Execute `rustlings hint intro2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {
println!("Hello {}!");
println!("Hello {}!", "world");
}
6 changes: 3 additions & 3 deletions exercises/move_semantics/move_semantics1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


#[test]
fn main() {
let vec0 = vec![22, 44, 66];

let vec1 = fill_vec(vec0);

//let a = vec0[0];
assert_eq!(vec1, vec![22, 44, 66, 88]);
}

fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec;
let mut vec = vec;

vec.push(88);

Expand Down
8 changes: 4 additions & 4 deletions exercises/move_semantics/move_semantics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


#[test]
fn main() {
let vec0 = vec![22, 44, 66];

let mut vec1 = fill_vec(vec0);
let vec1 = fill_vec(&vec0);

assert_eq!(vec0, vec![22, 44, 66]);
assert_eq!(vec1, vec![22, 44, 66, 88]);
}

fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
fn fill_vec(vec: &Vec<i32>) -> Vec<i32> {
let mut vec = vec.clone();

vec.push(88);

Expand Down
4 changes: 2 additions & 2 deletions exercises/move_semantics/move_semantics3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


#[test]
fn main() {
Expand All @@ -17,7 +17,7 @@ fn main() {
assert_eq!(vec1, vec![22, 44, 66, 88]);
}

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

vec
Expand Down
8 changes: 4 additions & 4 deletions exercises/move_semantics/move_semantics4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


#[test]
fn main() {
let vec0 = vec![22, 44, 66];
//let vec0 = vec![22, 44, 66];

let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec();

assert_eq!(vec1, vec![22, 44, 66, 88]);
}

// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
fn fill_vec() -> Vec<i32> {
// Instead, let's create and fill the Vec in here - how do you do that?
let mut vec = vec;
let mut vec = vec![22, 44, 66];

vec.push(88);

Expand Down
4 changes: 2 additions & 2 deletions exercises/move_semantics/move_semantics5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


#[test]
fn main() {
let mut x = 100;
let y = &mut x;
let z = &mut x;
*y += 100;
let z = &mut x;
*z += 1000;
assert_eq!(x, 1200);
}
12 changes: 6 additions & 6 deletions exercises/move_semantics/move_semantics6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


fn main() {
let data = "Rust is great!".to_string();

get_char(data);
get_char(&data);

string_uppercase(&data);
string_uppercase(data);
}

// Should not 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) {
data = &data.to_uppercase();
fn string_uppercase(mut data: String) {
data = data.to_uppercase();

println!("{}", data);
}
6 changes: 3 additions & 3 deletions exercises/primitive_types/primitive_types1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// Fill in the rest of the line that has code missing! No hints, there's no
// tricks, just get used to typing these :)

// I AM NOT DONE


fn main() {
// Booleans (`bool`)

let is_morning = true;
let is_morning = false;
if is_morning {
println!("Good morning!");
}

let // Finish the rest of this line like the example! Or make it be false!
let is_evening = ! is_morning;// Finish the rest of this line like the example! Or make it be false!
if is_evening {
println!("Good evening!");
}
Expand Down
Loading