This commit is contained in:
tab 2020-05-05 23:09:21 +08:00
parent c4cd0812df
commit c897a13a98
4 changed files with 6 additions and 12 deletions

View File

@ -6,14 +6,13 @@
// this function to have. // this function to have.
// Execute `rustlings hint errors1` for hints! // 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) -> Option<String> {
if name.len() > 0 { if name.len() > 0 {
Some(format!("Hi! My name is {}", name)) Some(format!("Hi! My name is {}", name))
} else { } else {
// Empty names aren't allowed. // Empty names aren't allowed.
None Some(format!("`name` was empty; it must be nonempty."))
// None
} }
} }
@ -36,7 +35,7 @@ mod tests {
fn explains_why_generating_nametag_text_fails() { fn explains_why_generating_nametag_text_fails() {
assert_eq!( assert_eq!(
generate_nametag_text("".into()), generate_nametag_text("".into()),
Err("`name` was empty; it must be nonempty.".into()) Some("`name` was empty; it must be nonempty.".into())
); );
} }
} }

View File

@ -16,14 +16,12 @@
// There are at least two ways to implement this that are both correct-- but // 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. // one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways.
// I AM NOT DONE
use std::num::ParseIntError; use std::num::ParseIntError;
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> { pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1; let processing_fee = 1;
let cost_per_item = 5; let cost_per_item = 5;
let qty = item_quantity.parse::<i32>(); let qty = item_quantity.parse::<i32>()?;
Ok(qty * cost_per_item + processing_fee) Ok(qty * cost_per_item + processing_fee)
} }

View File

@ -4,11 +4,9 @@
// Why not? What should we do to fix it? // Why not? What should we do to fix it?
// Execute `rustlings hint errors3` for hints! // Execute `rustlings hint errors3` for hints!
// I AM NOT DONE
use std::num::ParseIntError; use std::num::ParseIntError;
fn main() { fn main() -> Result<i32, ParseIntError> {
let mut tokens = 100; let mut tokens = 100;
let pretend_user_input = "8"; let pretend_user_input = "8";
@ -20,6 +18,7 @@ fn main() {
tokens -= cost; tokens -= cost;
println!("You now have {} tokens.", tokens); println!("You now have {} tokens.", tokens);
} }
Ok(cost)
} }
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> { pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {

View File

@ -15,8 +15,6 @@
// //
// Execute `rustlings hint errorsn` for hints :) // Execute `rustlings hint errorsn` for hints :)
// I AM NOT DONE
use std::error; use std::error;
use std::fmt; use std::fmt;
use std::io; use std::io;