Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated TODO errors3.rs #2180

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions exercises/13_error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ fn main() {
let pretend_user_input = "8";

// Don't change this line.
let cost = total_cost(pretend_user_input)?;
let cost = total_cost(pretend_user_input);

if cost > tokens {
println!("You can't afford that many!");
} else {
tokens -= cost;
println!("You now have {tokens} tokens.");
match cost {
Ok(cost) => {
if cost > tokens {
println!("You can't afford that many!");
} else {
tokens -= cost;
println!("You now have {tokens} tokens.");
}
},
Err(error) => {
println!("Error calculating the total cost: {}", error)
}
}
}
29 changes: 19 additions & 10 deletions exercises/14_generics/generics2.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// generics2.rs
//
// This powerful wrapper provides the ability to store a positive integer value.
// TODO: Rewrite it using a generic so that it supports wrapping ANY type.
struct Wrapper {
value: u32,
// Rewrite it using generics so that it supports wrapping ANY type.

struct Wrapper<T> {
value: T
}

// TODO: Adapt the struct's implementation to be generic over the wrapped value.
impl Wrapper {
fn new(value: u32) -> Self {
impl <T> Wrapper<T> {
pub fn new(value: T) -> Self {
Wrapper { value }
}
}

fn main() {
// You can optionally experiment here.
// This empty main function is needed, because the compiler expects it,
// while rustlings don't use this function for execution.
}

#[cfg(test)]
Expand All @@ -21,11 +24,17 @@ mod tests {

#[test]
fn store_u32_in_wrapper() {
assert_eq!(Wrapper::new(42).value, 42);
assert_eq!(Wrapper::new(42u32).value, 42u32);
}

#[test]
fn store_string_in_wrapper() {
let x = Wrapper::new(String::from("Foo"));
assert_eq!(x.value, String::from("Foo"));
}

#[test]
fn store_str_in_wrapper() {
assert_eq!(Wrapper::new("Foo").value, "Foo");
fn store_f64_in_wrapper() {
assert_eq!(Wrapper::new(42.0_f64).value, 42.0_f64);
}
}
Loading