This commit is contained in:
matytan 2022-12-01 15:09:37 +08:00
parent cd49153d47
commit f56d07d9f2
2 changed files with 7 additions and 14 deletions

View File

@ -3,24 +3,25 @@
// 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
// Add the AsRef trait appropriately as a trait bound // Add the AsRef trait appropriately as a trait bound
fn byte_counter<T>(arg: T) -> usize { fn byte_counter<T>(arg: T) -> usize
where
T: AsRef<str>,
{
arg.as_ref().as_bytes().len() arg.as_ref().as_bytes().len()
} }
// Obtain the number of characters (not bytes) in the given argument // Obtain the number of characters (not bytes) in the given argument
// Add the AsRef trait appropriately as a trait bound // 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(). Add the trait bound as is appropriate and // Squares a number using as_mut(). Add the trait bound as is appropriate and
// implement the function body. // implement the function body.
fn num_sq<T>(arg: &mut T) { fn num_sq<T: AsMut<u32>>(arg: &mut T) {
??? *arg.as_mut() = arg.as_mut().pow(2);
} }
#[cfg(test)] #[cfg(test)]

View File

@ -62,14 +62,6 @@ 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> {
let mut a: [u8; 3] = [0, 0, 0]; let mut a: [u8; 3] = [0, 0, 0];
// arr.into_iter().enumerate().map(|(i, x)| {
// let t: u8 = match x.try_into() {
// Ok(i) => i,
// Err(err) => {
// return Err(IntoColorError::IntConversion);
// }
// };
// });
for (i, x) in arr.into_iter().enumerate() { for (i, x) in arr.into_iter().enumerate() {
let tmp: u8 = match x.to_owned().try_into() { let tmp: u8 = match x.to_owned().try_into() {
Ok(i) => i, Ok(i) => i,