more exercises

This commit is contained in:
Esteban Escobar 2023-01-26 23:08:47 -05:00
parent 0fe225974d
commit 9ebb0a54ad
5 changed files with 24 additions and 11 deletions

View File

@ -18,4 +18,8 @@ return results to new Vec<i32> vector
17) errors2 make sure you understand error propagation but also the long-form solution (just for understanding). "The ? placed after a Result value is defined to work in almost the same way as the match expressions we defined to handle the Result values in Listing 9-6. If the value of the Result is an Ok, the value inside the Ok will get returned from this expression, and the program will continue. If the value is an Err, the Err will be returned from the whole function as if we had used the return keyword so the error value gets propagated to the calling code."
18) errors3
19) HAVE to revisit error5 - > great pre-intro to Box<dyn `Trait`>
20) error6 map_err for transforming Err type in Result into a new error value of the same type.
21) Revisit error6 => HARD
22) trait3 -> very important for understanding default implementation and overrides. Also reread this page: https://doc.rust-lang.org/book/ch10-02-traits.html#default-implementations. Answer also here: https://users.rust-lang.org/t/rustlings-traits3-rs-question/80742/3
25) trait5 -> important to understand trait bounds with generics. Also be sure to understand the short format vs. clearer format and pros/cons

View File

@ -8,7 +8,6 @@
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::num::ParseIntError;
@ -23,6 +22,10 @@ impl ParsePosNonzeroError {
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
ParsePosNonzeroError::Creation(err)
}
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
ParsePosNonzeroError::ParseInt(err)
}
// TODO: add another error conversion function here.
// fn from_parseint...
}
@ -30,8 +33,14 @@ impl ParsePosNonzeroError {
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();
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
match s.parse() {
Ok(n) => match PositiveNonzeroInteger::new(n) {
Ok(n) => Ok(n),
Err(err) => Err(ParsePosNonzeroError::from_creation(err))
},
Err(err) => Err(ParsePosNonzeroError::from_parseint(err))
}
}
// Don't change anything below this line.

View File

@ -7,10 +7,11 @@
// Consider what you can add to the Licensed trait.
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub trait Licensed {
fn licensing_info(&self) -> String;
fn licensing_info(&self) -> String {
"Some information".to_string()
}
}
struct SomeSoftware {

View File

@ -4,8 +4,6 @@
// Don't change any line other than the marked one.
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub trait Licensed {
fn licensing_info(&self) -> String {
"some information".to_string()
@ -20,7 +18,7 @@ impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}
// YOU MAY ONLY CHANGE THE NEXT LINE
fn compare_license_types(software: ??, software_two: ??) -> bool {
fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool {
software.licensing_info() == software_two.licensing_info()
}

View File

@ -4,8 +4,6 @@
// Don't change any line other than the marked one.
// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub trait SomeTrait {
fn some_function(&self) -> bool {
true
@ -27,7 +25,10 @@ impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}
// YOU MAY ONLY CHANGE THE NEXT LINE
fn some_func(item: ??) -> bool {
fn some_func<T>(item: T) -> bool
where
T: SomeTrait + OtherTrait,
{
item.some_function() && item.other_function()
}