Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
baiyazi233 committed Apr 8, 2024
1 parent 29047d2 commit be76387
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 32 deletions.
8 changes: 4 additions & 4 deletions exercises/error_handling/errors1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
// 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("`name` was empty; it must be nonempty.".into())
} else {
Some(format!("Hi! My name is {}", name))
Ok(format!("Hi! My name is {}", name))
}
}

Expand Down
13 changes: 10 additions & 3 deletions exercises/error_handling/errors2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@
// 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>();

Ok(qty * cost_per_item + processing_fee)
match qty {
Ok(value) => {
Ok(value * cost_per_item + processing_fee)
}
Err(err) => {
Err(err)
}
}

}

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions exercises/error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
// 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).unwrap();

if cost > tokens {
println!("You can't afford that many!");
Expand All @@ -28,7 +28,7 @@ fn main() {
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>().unwrap();

Ok(qty * cost_per_item + processing_fee)
}
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,7 @@
// 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 +17,14 @@ 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))
if value > 0 {
Ok(PositiveNonzeroInteger(value as u64))
} else if value < 0 {
Err(CreationError::Negative)
} else {
Err(CreationError::Zero)
}

}
}

Expand Down
5 changes: 3 additions & 2 deletions exercises/error_handling/errors5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
// 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;
use std::error::Error;

// TODO: update the return type of `main()` to make this compile.
fn main() -> Result<(), Box<dyn ???>> {
fn main() -> Result<(), Box<dyn 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,7 @@
// 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 +26,15 @@ 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().map_err(ParsePosNonzeroError::from_parseint)?;
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/generics/generics1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// 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");
}
10 changes: 5 additions & 5 deletions exercises/generics/generics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
// 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: 4 additions & 1 deletion exercises/traits/traits1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


trait AppendBar {
fn append_bar(self) -> Self;
}

impl AppendBar for String {
// TODO: Implement `AppendBar` for type `String`.
fn append_bar(self) -> Self {
self + "Bar"
}
}

fn main() {
Expand Down
9 changes: 8 additions & 1 deletion exercises/traits/traits2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
//
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE


trait AppendBar {
fn append_bar(self) -> Self;
}

// TODO: Implement trait `AppendBar` for a vector of strings.

impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Self{
self.push("Bar".to_string());
self
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 11 additions & 3 deletions exercises/traits/traits3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


pub trait Licensed {
fn licensing_info(&self) -> String;
Expand All @@ -22,8 +22,16 @@ struct OtherSoftware {
version_number: String,
}

impl Licensed for SomeSoftware {} // Don't edit this line
impl Licensed for OtherSoftware {} // Don't edit this line
impl Licensed for SomeSoftware {
fn licensing_info(&self) -> String{
"Some information".to_string()
}
} // Don't edit this line
impl Licensed for OtherSoftware {
fn licensing_info(&self) -> String{
"Some information".to_string()
}
} // Don't edit this line

#[cfg(test)]
mod tests {
Expand Down
4 changes: 2 additions & 2 deletions exercises/traits/traits4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


pub trait Licensed {
fn licensing_info(&self) -> String {
Expand All @@ -23,7 +23,7 @@ impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}

// YOU MAY ONLY CHANGE THE NEXT LINE
fn compare_license_types(software: ??, software_two: ??) -> bool {
fn compare_license_types<T: Licensed, U: Licensed>(software: T , software_two: U) -> bool {
software.licensing_info() == software_two.licensing_info()
}

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

// I AM NOT DONE

pub trait SomeTrait {
fn some_function(&self) -> bool {
Expand All @@ -30,7 +29,7 @@ impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}

// YOU MAY ONLY CHANGE THE NEXT LINE
fn some_func(item: ??) -> bool {
fn some_func<T: SomeTrait + OtherTrait>(item: T) -> bool {
item.some_function() && item.other_function()
}

Expand Down

0 comments on commit be76387

Please sign in to comment.