Skip to content

Commit

Permalink
rustlings solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
irad100 committed Mar 2, 2020
1 parent 8b94790 commit 4a812fa
Show file tree
Hide file tree
Showing 52 changed files with 124 additions and 187 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added arc1
Binary file not shown.
4 changes: 1 addition & 3 deletions exercises/clippy/clippy1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
//
// Execute `rustlings hint clippy1` for hints :)

// I AM NOT DONE

fn main() {
let x = 1.2331f64;
let y = 1.2332f64;
if y != x {
if (y - x).abs() > 0.0 {
println!("Success!");
}
}
4 changes: 1 addition & 3 deletions exercises/clippy/clippy2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// clippy2.rs
// Make me compile! Execute `rustlings hint clippy2` for hints :)

// I AM NOT DONE

fn main() {
let mut res = 42;
let option = Some(12);
for x in option {
if let Some(x) = option {
res += x;
}
println!("{}", res);
Expand Down
7 changes: 4 additions & 3 deletions exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// enums1.rs
// Make me compile! Execute `rustlings hint enums1` for hints!

// 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
7 changes: 4 additions & 3 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// enums2.rs
// Make me compile! Execute `rustlings hint enums2` for hints!

// I AM NOT DONE

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

impl Message {
Expand Down
14 changes: 10 additions & 4 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// enums3.rs
// Address all the TODOs to make the tests pass!

// I AM NOT DONE

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

struct Point {
Expand Down Expand Up @@ -36,7 +37,12 @@ impl State {
}

fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
match message {
Message::ChangeColor(r,g,b) => self.change_color((r as u8, g as u8, b as u8)),
Message::Echo(s) => self.echo(s),
Message::Move{x,y} => self.move_position(Point{ x: x as u8, y: y as u8 }),
Message::Quit => self.quit()
}
}
}

Expand Down
10 changes: 4 additions & 6 deletions exercises/error_handling/errors1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
// this function to have.
// Execute `rustlings hint errors1` for hints!

// 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.len() > 0 {
Some(format!("Hi! My name is {}", name))
Ok(format!("Hi! My name is {}", name))
} else {
// Empty names aren't allowed.
None
Err(format!("`{}` was empty; it must be nonempty.", "name"))
}
}

Expand All @@ -28,7 +26,7 @@ mod tests {
fn generates_nametag_text_for_a_nonempty_name() {
assert_eq!(
generate_nametag_text("Beyoncé".into()),
Some("Hi! My name is Beyoncé".into())
Ok("Hi! My name is Beyoncé".into())
);
}

Expand Down
4 changes: 1 addition & 3 deletions exercises/error_handling/errors2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
// There are at least two ways to implement this that are both correct-- but
// one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways.

// 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
8 changes: 3 additions & 5 deletions exercises/error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@
// Why not? What should we do to fix it?
// Execute `rustlings hint errors3` for hints!

// I AM NOT DONE

use std::num::ParseIntError;

fn main() {
fn main() -> Result<String, ParseIntError> {
let mut tokens = 100;
let pretend_user_input = "8";

let cost = total_cost(pretend_user_input)?;

if cost > tokens {
println!("You can't afford that many!");
Ok(format!("You can't afford that many!"))
} else {
tokens -= cost;
println!("You now have {} tokens.", tokens);
Ok(format!("You now have {} tokens.", tokens))
}
}

Expand Down
12 changes: 5 additions & 7 deletions exercises/error_handling/errorsn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
//
// Execute `rustlings hint errorsn` for hints :)

// I AM NOT DONE

use std::error;
use std::fmt;
use std::io;

// PositiveNonzeroInteger is a struct defined below the tests.
fn read_and_validate(b: &mut dyn io::BufRead) -> Result<PositiveNonzeroInteger, ???> {
fn read_and_validate(b: &mut dyn io::BufRead) -> Result<PositiveNonzeroInteger, Box<dyn error::Error>> {
let mut line = String::new();
b.read_line(&mut line);
let num: i64 = line.trim().parse();
let answer = PositiveNonzeroInteger::new(num);
answer
b.read_line(&mut line)?;
let num: i64 = line.trim().parse()?;
let answer = PositiveNonzeroInteger::new(num)?;
Ok(answer)
}

// This is a test helper function that turns a &str into a BufReader.
Expand Down
18 changes: 9 additions & 9 deletions exercises/error_handling/option1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
// on `None`. Handle this in a more graceful way than calling `unwrap`!
// Execute `rustlings hint option1` for hints :)

// I AM NOT DONE

pub fn pop_too_much() -> bool {
let mut list = vec![3];

let last = list.pop().unwrap();
println!("The last item in the list is {:?}", last);
match list.pop() {
Some(item) => println!("The last item in the list is {:?}", item),
None => println!("Failed"),
};

match list.pop() {
Some(item) => println!("The second-to-last item in the list is {:?}", item),
None => println!("Failed"),
};

let second_to_last = list.pop().unwrap();
println!(
"The second-to-last item in the list is {:?}",
second_to_last
);
true
}

Expand Down
12 changes: 9 additions & 3 deletions exercises/error_handling/result1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// result1.rs
// Make this test pass! Execute `rustlings hint result1` for hints :)

// I AM NOT DONE

#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);

Expand All @@ -14,7 +12,15 @@ enum CreationError {

impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
Ok(PositiveNonzeroInteger(value as u64))
if value > 0 {
Ok(PositiveNonzeroInteger(value as u64))
}
else if value == 0 {
Err(CreationError::Zero)
}
else {
Err(CreationError::Negative)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion exercises/functions/functions1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// functions1.rs
// Make me compile! Execute `rustlings hint functions1` for hints :)

// I AM NOT DONE
pub fn call_me() {
println!("test");
}

fn main() {
call_me();
Expand Down
4 changes: 1 addition & 3 deletions exercises/functions/functions2.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// functions2.rs
// Make me compile! Execute `rustlings hint functions2` for hints :)

// I AM NOT DONE

fn main() {
call_me(3);
}

fn call_me(num) {
fn call_me(num: i32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
Expand Down
4 changes: 1 addition & 3 deletions exercises/functions/functions3.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// functions3.rs
// Make me compile! Execute `rustlings hint functions3` for hints :)

// I AM NOT DONE

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

fn call_me(num: i32) {
Expand Down
4 changes: 1 addition & 3 deletions exercises/functions/functions4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
// This store is having a sale where if the price is an even number, you get
// 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.

// 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
4 changes: 1 addition & 3 deletions exercises/functions/functions5.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// functions5.rs
// Make me compile! Execute `rustlings hint functions5` for hints :)

// I AM NOT DONE

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

fn square(num: i32) -> i32 {
num * num;
num * num
}
7 changes: 1 addition & 6 deletions exercises/if/if1.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
// if1.rs

// 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
// Execute `rustlings hint if1` for hints
if a > b { a } else { b }
}

// Don't mind this for now :)
Expand Down
4 changes: 1 addition & 3 deletions exercises/macros/macros1.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// macros1.rs
// Make me compile! Execute `rustlings hint macros1` for hints :)

// I AM NOT DONE

macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}

fn main() {
my_macro();
my_macro!();
}
10 changes: 4 additions & 6 deletions exercises/macros/macros2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// macros2.rs
// Make me compile! Execute `rustlings hint macros2` for hints :)

// I AM NOT DONE

fn main() {
my_macro!();
}

macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}

fn main() {
my_macro!();
}
3 changes: 1 addition & 2 deletions exercises/macros/macros3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Make me compile, without taking the macro out of the module!
// Execute `rustlings hint macros3` for hints :)

// I AM NOT DONE

#[macro_use]
mod macros {
macro_rules! my_macro {
() => {
Expand Down
4 changes: 1 addition & 3 deletions exercises/macros/macros4.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// macros4.rs
// Make me compile! Execute `rustlings hint macros4` for hints :)

// I AM NOT DONE

macro_rules! my_macro {
() => {
println!("Check out my macro!");
}
};
($val:expr) => {
println!("Look at this other macro: {}", $val);
}
Expand Down
Loading

0 comments on commit 4a812fa

Please sign in to comment.