New progress
This commit is contained in:
parent
5004cc3bbb
commit
5f8d837c7d
@ -5,14 +5,13 @@
|
|||||||
// construct to `Option` that can be used to express error conditions. Let's use it!
|
// construct to `Option` that can be used to express error conditions. Let's use it!
|
||||||
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a hint.
|
// 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() {
|
if name.is_empty() {
|
||||||
// Empty names aren't allowed.
|
// Empty names aren't allowed.
|
||||||
None
|
Err("`name` was empty; it must be nonempty.".into())
|
||||||
} else {
|
} else {
|
||||||
Some(format!("Hi! My name is {}", name))
|
Ok(format!("Hi! My name is {}", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,14 +17,13 @@
|
|||||||
// one is a lot shorter!
|
// one is a lot shorter!
|
||||||
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// 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)
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,10 @@
|
|||||||
// Why not? What should we do to fix it?
|
// Why not? What should we do to fix it?
|
||||||
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), ParseIntError> {
|
||||||
let mut tokens = 100;
|
let mut tokens = 100;
|
||||||
let pretend_user_input = "8";
|
let pretend_user_input = "8";
|
||||||
|
|
||||||
@ -20,6 +19,7 @@ fn main() {
|
|||||||
tokens -= cost;
|
tokens -= cost;
|
||||||
println!("You now have {} tokens.", tokens);
|
println!("You now have {} tokens.", tokens);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// errors4.rs
|
// errors4.rs
|
||||||
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
@ -15,6 +14,8 @@ enum CreationError {
|
|||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||||
// Hmm...? Why is this only returning an Ok value?
|
// Hmm...? Why is this only returning an Ok value?
|
||||||
|
if value < 0 { return Err(CreationError::Negative); }
|
||||||
|
if value == 0 { return Err(CreationError::Zero); }
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
Ok(PositiveNonzeroInteger(value as u64))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,14 +16,13 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
// TODO: update the return type of `main()` to make this compile.
|
// 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 pretend_user_input = "42";
|
||||||
let x: i64 = pretend_user_input.parse()?;
|
let x: i64 = pretend_user_input.parse()?;
|
||||||
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
@ -25,12 +24,15 @@ impl ParsePosNonzeroError {
|
|||||||
}
|
}
|
||||||
// TODO: add another error conversion function here.
|
// TODO: add another error conversion function here.
|
||||||
// fn from_parseint...
|
// fn from_parseint...
|
||||||
|
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
|
||||||
|
ParsePosNonzeroError::ParseInt(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
|
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
|
||||||
// TODO: change this to return an appropriate error instead of panicking
|
// TODO: change this to return an appropriate error instead of panicking
|
||||||
// when `parse()` returns an error.
|
// 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)
|
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,9 +3,8 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut shopping_list: Vec<?> = Vec::new();
|
let mut shopping_list: Vec<&str> = Vec::new();
|
||||||
shopping_list.push("milk");
|
shopping_list.push("milk");
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,13 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct Wrapper {
|
struct Wrapper<T> {
|
||||||
value: u32,
|
value: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Wrapper {
|
impl<T> Wrapper<T> {
|
||||||
pub fn new(value: u32) -> Self {
|
pub fn new(value: T) -> Self {
|
||||||
Wrapper { value }
|
Wrapper { value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// options3.rs
|
// options3.rs
|
||||||
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
x: i32,
|
x: i32,
|
||||||
@ -12,7 +11,7 @@ fn main() {
|
|||||||
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
||||||
|
|
||||||
match y {
|
match y {
|
||||||
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
|
||||||
_ => println!("no match"),
|
_ => println!("no match"),
|
||||||
}
|
}
|
||||||
y; // Fix without deleting this line.
|
y; // Fix without deleting this line.
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
// implementing this trait.
|
// implementing this trait.
|
||||||
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
trait AppendBar {
|
trait AppendBar {
|
||||||
fn append_bar(self) -> Self;
|
fn append_bar(self) -> Self;
|
||||||
@ -17,6 +16,9 @@ trait AppendBar {
|
|||||||
|
|
||||||
impl AppendBar for String {
|
impl AppendBar for String {
|
||||||
// TODO: Implement `AppendBar` for type `String`.
|
// TODO: Implement `AppendBar` for type `String`.
|
||||||
|
fn append_bar(self) -> Self {
|
||||||
|
format!("{}Bar", self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -11,13 +11,18 @@
|
|||||||
// you can do this!
|
// you can do this!
|
||||||
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
trait AppendBar {
|
trait AppendBar {
|
||||||
fn append_bar(self) -> Self;
|
fn append_bar(self) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement trait `AppendBar` for a vector of strings.
|
// TODO: Implement trait `AppendBar` for a vector of strings.
|
||||||
|
impl AppendBar for Vec<String> {
|
||||||
|
fn append_bar(mut self) -> Self {
|
||||||
|
self.push(String::from("Bar"));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
Loading…
Reference in New Issue
Block a user