Ejercicios 57/94
This commit is contained in:
parent
4f57728dd9
commit
5c4f61218f
|
@ -5,14 +5,12 @@
|
||||||
// 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) -> Result<String, String> {
|
||||||
|
|
||||||
pub fn generate_nametag_text(name: String) -> Option<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.".to_string())
|
||||||
} else {
|
} else {
|
||||||
Some(format!("Hi! My name is {}", name))
|
Ok(format!("Hi! My name is {}", name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,14 +17,12 @@
|
||||||
// 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,9 @@
|
||||||
// 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 +18,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,8 +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);
|
||||||
|
|
||||||
|
@ -14,8 +12,13 @@ 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?
|
if value < 0 {
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
Err(CreationError::Negative)
|
||||||
|
} else if value == 0 {
|
||||||
|
Err(CreationError::Zero)
|
||||||
|
}else {
|
||||||
|
Ok(PositiveNonzeroInteger(value as u64))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,14 +16,12 @@
|
||||||
|
|
||||||
// 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,8 +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;
|
||||||
|
|
||||||
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
|
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
|
||||||
|
@ -23,14 +21,13 @@ impl ParsePosNonzeroError {
|
||||||
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
|
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
|
||||||
ParsePosNonzeroError::Creation(err)
|
ParsePosNonzeroError::Creation(err)
|
||||||
}
|
}
|
||||||
// TODO: add another error conversion function here.
|
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
|
||||||
// fn from_parseint...
|
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
|
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
|
||||||
// when `parse()` returns an error.
|
|
||||||
let x: i64 = s.parse().unwrap();
|
|
||||||
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
// options1.rs
|
// options1.rs
|
||||||
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
// This function returns how much icecream there is left in the fridge.
|
// This function returns how much icecream there is left in the fridge.
|
||||||
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
|
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
|
||||||
// all, so there'll be no more left :(
|
// all, so there'll be no more left :(
|
||||||
fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
||||||
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0
|
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0
|
||||||
// The Option output should gracefully handle cases where time_of_day > 23.
|
// The Option output should gracefully handle cases where time_of_day > 23.
|
||||||
// TODO: Complete the function body - remember to return an Option!
|
if time_of_day < 22 {
|
||||||
???
|
Some(5)
|
||||||
|
} else if time_of_day < 24 {
|
||||||
|
Some(0)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -28,8 +31,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn raw_value() {
|
fn raw_value() {
|
||||||
// TODO: Fix this test. How do you get at the value contained in the Option?
|
|
||||||
let icecreams = maybe_icecream(12);
|
let icecreams = maybe_icecream(12);
|
||||||
assert_eq!(icecreams, 5);
|
assert_eq!(icecreams, Some(5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
// options2.rs
|
// options2.rs
|
||||||
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -10,8 +8,7 @@ mod tests {
|
||||||
let target = "rustlings";
|
let target = "rustlings";
|
||||||
let optional_target = Some(target);
|
let optional_target = Some(target);
|
||||||
|
|
||||||
// TODO: Make this an if let statement whose value is "Some" type
|
if let Some(word) = optional_target {
|
||||||
word = optional_target {
|
|
||||||
assert_eq!(word, target);
|
assert_eq!(word, target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,10 +21,8 @@ mod tests {
|
||||||
optional_integers.push(Some(i));
|
optional_integers.push(Some(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
|
while let Some(integer) = optional_integers.pop() {
|
||||||
// You can stack `Option<T>`'s into while let and if let
|
assert_eq!(integer, Some(range));
|
||||||
integer = optional_integers.pop() {
|
|
||||||
assert_eq!(integer, range);
|
|
||||||
range -= 1;
|
range -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +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,
|
||||||
y: i32,
|
y: i32,
|
||||||
|
@ -12,7 +10,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.
|
||||||
|
|
Loading…
Reference in New Issue