Skip to content
6 changes: 4 additions & 2 deletions exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
//
// 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
6 changes: 4 additions & 2 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// 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: i32, y: i32},
Echo(String),
ChangeColor(i32, i32, i32),
Quit
}

impl Message {
Expand Down
12 changes: 10 additions & 2 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

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 +47,12 @@ 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::Echo(echo) => self.echo(echo),
Message::ChangeColor(red, green, blue) => self.change_color((red,green,blue)),
Message::Quit => self.quit(),
}
}
}

Expand Down
8 changes: 3 additions & 5 deletions exercises/error_handling/errors1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

pub fn generate_nametag_text(name: String) -> Option<String> {
pub fn generate_nametag_text(name: String) -> Result<String, String> {
if name.is_empty() {
// Empty names aren't allowed.
None
Err(format!("`name` was empty; it must be nonempty."))
} else {
Some(format!("Hi! My name is {}", name))
Ok(format!("Hi! My name is {}", name))
}
}

Expand Down
3 changes: 1 addition & 2 deletions exercises/error_handling/errors2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::num::ParseIntError;

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>();
let qty = item_quantity.parse::<i32>()?;

Ok(qty * cost_per_item + processing_fee)
}
Expand Down
3 changes: 1 addition & 2 deletions exercises/error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::num::ParseIntError;

fn main() {
let mut tokens = 100;
let pretend_user_input = "8";

let cost = total_cost(pretend_user_input)?;
let cost = total_cost(pretend_user_input).expect("Parse Int Error");

if cost > tokens {
println!("You can't afford that many!");
Expand Down
8 changes: 5 additions & 3 deletions exercises/error_handling/errors4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);
Expand All @@ -16,8 +15,11 @@ enum CreationError {

impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
// Hmm... Why is this always returning an Ok value?
Ok(PositiveNonzeroInteger(value as u64))
match value.signum() {
-1 => Err(CreationError::Negative),
0 => Err(CreationError::Zero),
_ => Ok(PositiveNonzeroInteger(value as u64))
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions exercises/error_handling/errors5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::error;
use std::fmt;
use std::num::ParseIntError;

// TODO: update the return type of `main()` to make this compile.
fn main() -> Result<(), Box<dyn ???>> {
fn main() -> Result<(), Box<dyn error::Error>> {
let pretend_user_input = "42";
let x: i64 = pretend_user_input.parse()?;
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
Expand Down
7 changes: 5 additions & 2 deletions exercises/error_handling/errors6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::num::ParseIntError;

Expand All @@ -26,12 +25,16 @@ impl ParsePosNonzeroError {
}
// TODO: add another error conversion function here.
// fn from_parseint...
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
ParsePosNonzeroError::ParseInt(err)
}
}

fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
// TODO: change this to return an appropriate error instead of panicking
// when `parse()` returns an error.
let x: i64 = s.parse().unwrap();
//let x: i64 = s.parse().unwrap();
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
}

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

// I AM NOT DONE

fn main() {
call_me();
}

fn call_me(){}
3 changes: 1 addition & 2 deletions exercises/functions/functions2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// 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: isize) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
Expand Down
3 changes: 1 addition & 2 deletions exercises/functions/functions3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
// 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) {
Expand Down
3 changes: 1 addition & 2 deletions exercises/functions/functions4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

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

fn sale_price(price: i32) -> {
fn sale_price(price: i32) -> i32 {
if is_even(price) {
price - 10
} else {
Expand Down
3 changes: 1 addition & 2 deletions exercises/functions/functions5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// 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
}
3 changes: 1 addition & 2 deletions exercises/generics/generics1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn main() {
let mut shopping_list: Vec<?> = Vec::new();
let mut shopping_list: Vec<&str> = Vec::new();
shopping_list.push("milk");
}
9 changes: 4 additions & 5 deletions exercises/generics/generics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

struct Wrapper {
value: u32,
struct Wrapper<T> {
value: T,
}

impl Wrapper {
pub fn new(value: u32) -> Self {
impl<T> Wrapper<T> {
pub fn new(value: T) -> Self {
Wrapper { value }
}
}
Expand Down
5 changes: 3 additions & 2 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::collections::HashMap;

fn fruit_basket() -> HashMap<String, u32> {
let mut basket = // TODO: declare your hash map here.
let mut basket = HashMap::new(); // TODO: declare your hash map here.

// Two bananas are already given for you :)
basket.insert(String::from("banana"), 2);

// TODO: Put more fruits in your basket here.

basket.insert(String::from("apple"), 3);
basket.insert(String::from("mango"), 5);
basket
}

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

// I AM NOT DONE

use std::collections::HashMap;

Expand All @@ -40,7 +39,11 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// TODO: Insert new fruits if they are not already present in the
// basket. Note that you are not allowed to put any type of fruit that's
// already present!
if fruit == Fruit::Banana || fruit == Fruit::Pineapple {
basket.insert(fruit, 1);
}
}

}

#[cfg(test)]
Expand Down
30 changes: 29 additions & 1 deletion exercises/hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::collections::HashMap;

Expand All @@ -39,6 +38,35 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded from team_2, and similarly
// goals scored by team_2 will be the number of goals conceded by
// team_1.
let mut team_1 = Team{
goals_scored: team_1_score,
goals_conceded: team_2_score
};

let mut team_2 = Team{
goals_scored: team_2_score,
goals_conceded: team_1_score
};

match scores.get(&team_1_name) {
None => {scores.insert(team_1_name, team_1);},

Some(t) => {
team_1.goals_scored += t.goals_scored;
team_1.goals_conceded += t.goals_conceded;
scores.insert(team_1_name, team_1);
}
};

match scores.get(&team_2_name) {
None => {scores.insert(team_2_name, team_2);},

Some(t) => {
team_2.goals_scored += t.goals_scored;
team_2.goals_conceded += t.goals_conceded;
scores.insert(team_2_name, team_2);
}
};
}
scores
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/if/if1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
//
// 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 {a} else {b}
}

// Don't mind this for now :)
Expand Down
7 changes: 4 additions & 3 deletions exercises/if/if2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +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"
}
}

// No test changes needed!
Expand Down
Loading