Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
saiyuan233 committed Dec 25, 2024
1 parent 296bbba commit 49a9e3e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
12 changes: 10 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,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))
if value <= 0 {
if value < 0 {
Err(CreationError::Negative)
} else {
Err(CreationError::Zero)
}
} else {
Ok(PositiveNonzeroInteger(value as u64))
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/error_handling/errors5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
// 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
8 changes: 5 additions & 3 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 @@ -25,13 +25,15 @@ 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 = 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
8 changes: 7 additions & 1 deletion exercises/traits/traits2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
// I AM NOT DONE

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

// TODO: Implement trait `AppendBar` for a vector of strings.
impl AppendBar for Vec<String> {
fn append_bar(&mut self) {
self.push(String::from("Bar"));
self
}
}

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 49a9e3e

Please sign in to comment.