Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yyheroi committed Jul 23, 2024
1 parent af0df3a commit 44869a0
Show file tree
Hide file tree
Showing 91 changed files with 372 additions and 268 deletions.
3 changes: 1 addition & 2 deletions exercises/clippy/clippy1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::f32;

fn main() {
let pi = 3.14f32;
let pi = f32::consts::PI;
let radius = 5.00f32;

let area = pi * f32::powi(radius, 2);
Expand Down
3 changes: 1 addition & 2 deletions exercises/clippy/clippy2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a
// hint.

// 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
15 changes: 6 additions & 9 deletions exercises/clippy/clippy3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@
//
// Execute `rustlings hint clippy3` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

#[allow(unused_variables, unused_assignments)]
fn main() {
let my_option: Option<()> = None;
if my_option.is_none() {
my_option.unwrap();
// my_option.unwrap();
}

let my_arr = &[
-1, -2, -3
-4, -5, -6
];
let my_arr = &[ -1, -2, -3, -4, -5, -6, ];
println!("My array! Here it is: {:?}", my_arr);

let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5);
let mut my_empty_vec = vec![1, 2, 3, 4, 5];
my_empty_vec.clear();
println!("This Vec is empty, see? {:?}", my_empty_vec);

let mut value_a = 45;
let mut value_b = 66;
// Let's swap these two!
value_a = value_b;
value_b = value_a;
std::mem::swap(&mut value_a, &mut value_b);

println!("value a: {}; value b: {}", value_a, value_b);
}
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
12 changes: 10 additions & 2 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

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

impl Message {
Expand All @@ -18,7 +26,7 @@ impl Message {

fn main() {
let messages = [
Message::Move { x: 10, y: 30 },
Message::Move(MoveXY{x: 10, y: 30}),
Message::Echo(String::from("hello world")),
Message::ChangeColor(200, 255, 255),
Message::Quit,
Expand Down
11 changes: 10 additions & 1 deletion exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
// 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 @@ -43,6 +46,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::ChangeColor(a, b, c) => self.change_color((a, b, c)),
Message::Echo(s) => self.echo(s),
Message::Move(p) => self.move_position(p),
Message::Quit => self.quit(),
}
}
}

Expand Down
7 changes: 3 additions & 4 deletions exercises/error_handling/errors1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
// 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, &'static str> {
if name.is_empty() {
// Empty names aren't allowed.
None
Err("`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
7 changes: 3 additions & 4 deletions exercises/error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::num::ParseIntError;

fn main() {
fn main() -> Result<(), 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(println!("You can't afford that many!"))
} else {
tokens -= cost;
println!("You now have {} tokens.", tokens);
Ok(println!("You now have {} tokens.", tokens))
}
}

Expand Down
11 changes: 9 additions & 2 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 @@ -17,7 +16,15 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
// Hmm...? Why is this only returning an Ok value?
Ok(PositiveNonzeroInteger(value as u64))
match value {
10_i64 =>
Ok(PositiveNonzeroInteger(value as u64)),
-10_i64 =>
Err(CreationError::Negative),
0 =>
Err(CreationError::Zero),
_ => todo!(),
}
}
}

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
11 changes: 8 additions & 3 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 @@ -25,13 +24,19 @@ impl ParsePosNonzeroError {
ParsePosNonzeroError::Creation(err)
}
// 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 = match s.parse() {
Err(e) => return Err(ParsePosNonzeroError::from_parseint(e)),
Ok(k) => k,
};

PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
}

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

// I AM NOT DONE

fn call_me(){}
fn main() {
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:i32) {
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(2);
}

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
7 changes: 4 additions & 3 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
// 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);

basket.insert(String::from("banana1"), 3);
basket.insert(String::from("banana2"), 4);
basket.insert(String::from("banana3"), 5);
// TODO: Put more fruits in your basket here.

basket
Expand Down
Loading

0 comments on commit 44869a0

Please sign in to comment.