gitignore updates

This commit is contained in:
Esteban Escobar 2023-02-21 22:21:39 -05:00
parent e5225cb7c8
commit 48e5577f2d
5 changed files with 87 additions and 14 deletions

2
.gitignore vendored
View File

@ -11,3 +11,5 @@ rust-project.json
!.vscode/extensions.json !.vscode/extensions.json
*.iml *.iml
*.o *.o
REVISITS.md

View File

@ -34,7 +34,16 @@ return results to new Vec<i32> vector
35) All threads exercises :(. handles and join methods for thread completion wait in the main thread. 35) All threads exercises :(. handles and join methods for thread completion wait in the main thread.
36) threads2 exercises important for understanding synchorization primitize Mutex 36) threads2 exercises important for understanding synchorization primitize Mutex
37) threads3 - again - all threads exercises. 37) threads3 - again - all threads exercises.
38) all macros exercises 38) all macros
39) Conversions from_str be sure to understand map_err() for error projection!
40) All conversions man:
```let mut x = 5;
let x_ref = &mut x;
conversions let x_ref2 = x_ref.as_mut(); // convert to mutable reference
*x_ref2 = 10; // modify the value
assert_eq!(x, 10);
In this example, x_ref is a mutable reference to x. We can use the as_mut method to convert x_ref to a mutable reference, which we store in x_ref2. We can then use x_ref2 to modify the value of x.```

View File

@ -3,25 +3,26 @@
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively. // and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
// Obtain the number of bytes (not characters) in the given argument. // Obtain the number of bytes (not characters) in the given argument.
// TODO: Add the AsRef trait appropriately as a trait bound. // TODO: Add the AsRef trait appropriately as a trait bound.
fn byte_counter<T>(arg: T) -> usize { fn byte_counter<T: AsRef<[u8]>>(arg: T) -> usize {
arg.as_ref().as_bytes().len() arg.as_ref().len()
} }
// Obtain the number of characters (not bytes) in the given argument. // Obtain the number of characters (not bytes) in the given argument.
// TODO: Add the AsRef trait appropriately as a trait bound. // TODO: Add the AsRef trait appropriately as a trait bound.
fn char_counter<T>(arg: T) -> usize { fn char_counter<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().chars().count() arg.as_ref().chars().count()
} }
// Squares a number using as_mut(). // Squares a number using as_mut().
// TODO: Add the appropriate trait bound. // TODO: Add the appropriate trait bound.
fn num_sq<T>(arg: &mut T) { fn num_sq<T>(arg: &mut T)
where
T: AsMut<u32>,
{
// TODO: Implement the function body. // TODO: Implement the function body.
??? *arg.as_mut() *= *arg.as_mut();
} }
#[cfg(test)] #[cfg(test)]

View File

@ -28,8 +28,6 @@ enum ParsePersonError {
ParseInt(ParseIntError), ParseInt(ParseIntError),
} }
// I AM NOT DONE
// Steps: // Steps:
// 1. If the length of the provided string is 0, an error should be returned // 1. If the length of the provided string is 0, an error should be returned
// 2. Split the given string on the commas present in it // 2. Split the given string on the commas present in it
@ -46,8 +44,30 @@ enum ParsePersonError {
impl FromStr for Person { impl FromStr for Person {
type Err = ParsePersonError; type Err = ParsePersonError;
fn from_str(s: &str) -> Result<Person, Self::Err> { fn from_str(s: &str) -> Result<Person, Self::Err> {
if s.is_empty() {
return Err(ParsePersonError::Empty);
} else {
let mycollection: Vec<&str> = s.split(",").collect();
if mycollection[0].is_empty() {
return Err(ParsePersonError::NoName);
} else if mycollection.len() != 2 {
return Err(ParsePersonError::BadLen);
} else {
let myage = mycollection[1].parse::<usize>().map_err(ParsePersonError::ParseInt)?;
let myname = mycollection[0].to_string();
Ok(Person{
age: myage,
name: myname
})
}
}
}
} }
}
fn main() { fn main() {
let p = "Mark,20".parse::<Person>().unwrap(); let p = "Mark,20".parse::<Person>().unwrap();

View File

@ -23,8 +23,6 @@ enum IntoColorError {
IntConversion, IntConversion,
} }
// I AM NOT DONE
// Your task is to complete this implementation // Your task is to complete this implementation
// and return an Ok result of inner type Color. // and return an Ok result of inner type Color.
// You need to create an implementation for a tuple of three integers, // You need to create an implementation for a tuple of three integers,
@ -38,6 +36,23 @@ enum IntoColorError {
impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<(i16, i16, i16)> for Color {
type Error = IntoColorError; type Error = IntoColorError;
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> { fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
if tuple.0 < 0 || tuple.0 > 255 ||
tuple.1 < 0 || tuple.1 > 255 ||
tuple.2 < 0 || tuple.2 > 255 {
return Err(IntoColorError::IntConversion);
} else {
Ok(Color{
red: tuple.0 as u8,
green: tuple.1 as u8,
blue: tuple.2 as u8
})
}
} }
} }
@ -45,6 +60,18 @@ impl TryFrom<(i16, i16, i16)> for Color {
impl TryFrom<[i16; 3]> for Color { impl TryFrom<[i16; 3]> for Color {
type Error = IntoColorError; type Error = IntoColorError;
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> { fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
if arr[0] < 0 || arr[0] > 255 ||
arr[1] < 0 || arr[1] > 255 ||
arr[2] < 0 || arr[1] > 255 {
return Err(IntoColorError::IntConversion);
} else {
Ok(Color{
red:arr[0] as u8,
green:arr[1] as u8,
blue: arr[2] as u8
})
}
} }
} }
@ -52,6 +79,20 @@ impl TryFrom<[i16; 3]> for Color {
impl TryFrom<&[i16]> for Color { impl TryFrom<&[i16]> for Color {
type Error = IntoColorError; type Error = IntoColorError;
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> { fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
if slice.len() != 3 {
return Err(IntoColorError::BadLen);
} else if slice[0] < 0 || slice[0] > 255 ||
slice[1] < 0 || slice[1] > 255 ||
slice[2] < 0 || slice[2] > 255 {
return Err(IntoColorError::IntConversion)
} else {
Ok(Color{
red: slice[0] as u8,
green: slice[1] as u8,
blue: slice[2] as u8
})
}
} }
} }