Compare commits
2 Commits
5ac6421498
...
603a6c27ac
Author | SHA1 | Date |
---|---|---|
perro tuerto | 603a6c27ac | |
perro tuerto | 27661c5c6a |
|
@ -3,25 +3,19 @@
|
|||
// 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.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Obtain the number of bytes (not characters) in the given argument.
|
||||
// TODO: Add the AsRef trait appropriately as a trait bound.
|
||||
fn byte_counter<T>(arg: T) -> usize {
|
||||
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
|
||||
arg.as_ref().as_bytes().len()
|
||||
}
|
||||
|
||||
// Obtain the number of characters (not bytes) in the given argument.
|
||||
// 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()
|
||||
}
|
||||
|
||||
// Squares a number using as_mut().
|
||||
// TODO: Add the appropriate trait bound.
|
||||
fn num_sq<T>(arg: &mut T) {
|
||||
// TODO: Implement the function body.
|
||||
???
|
||||
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
|
||||
*arg.as_mut() = *arg.as_mut() * *arg.as_mut();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -35,10 +35,16 @@ impl Default for Person {
|
|||
// If while parsing the age, something goes wrong, then return the default of Person
|
||||
// Otherwise, then return an instantiated Person object with the results
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
impl From<&str> for Person {
|
||||
fn from(s: &str) -> Person {
|
||||
let vec: Vec<&str> = s.split(",").collect();
|
||||
let name = vec[0].trim();
|
||||
let age = vec[vec.len() - 1].parse::<usize>();
|
||||
if vec.len() == 2 && name.len() > 0 && age.is_ok() {
|
||||
Person { name: name.to_string(), age: age.unwrap() }
|
||||
} else {
|
||||
Person::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,8 +28,6 @@ enum ParsePersonError {
|
|||
ParseInt(ParseIntError),
|
||||
}
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Steps:
|
||||
// 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
|
||||
|
@ -46,6 +44,22 @@ enum ParsePersonError {
|
|||
impl FromStr for Person {
|
||||
type Err = ParsePersonError;
|
||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||
let vec: Vec<&str> = s.split(",").collect();
|
||||
let name = vec[0].trim();
|
||||
let age = vec[vec.len() - 1].parse::<usize>();
|
||||
if vec.len() == 2 && name.len() > 0 && age.is_ok() {
|
||||
Ok(Person { name: name.to_string(), age: age.unwrap() })
|
||||
} else {
|
||||
if s.trim().len() == 0 {
|
||||
Err(ParsePersonError::Empty)
|
||||
} else if vec.len() != 2 {
|
||||
Err(ParsePersonError::BadLen)
|
||||
} else if name.len() == 0 {
|
||||
Err(ParsePersonError::NoName)
|
||||
} else {
|
||||
Err(ParsePersonError::ParseInt(age.unwrap_err()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,8 +23,6 @@ enum IntoColorError {
|
|||
IntConversion,
|
||||
}
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Your task is to complete this implementation
|
||||
// and return an Ok result of inner type Color.
|
||||
// You need to create an implementation for a tuple of three integers,
|
||||
|
@ -34,10 +32,30 @@ enum IntoColorError {
|
|||
// but the slice implementation needs to check the slice length!
|
||||
// Also note that correct RGB color values must be integers in the 0..=255 range.
|
||||
|
||||
fn into_color(collection: Vec<i16>) -> Result<Color, IntoColorError> {
|
||||
let mut color = Color {red: 0, green: 0, blue: 0};
|
||||
if collection.len() < 3 { return Err(IntoColorError::BadLen) }
|
||||
for (i, num) in collection.iter().enumerate() {
|
||||
if *num >= 0 && *num <= 255 {
|
||||
let val = *num as u8;
|
||||
match i {
|
||||
0 => color.red = val,
|
||||
1 => color.green = val,
|
||||
2 => color.blue = val,
|
||||
_ => return Err(IntoColorError::BadLen),
|
||||
}
|
||||
} else {
|
||||
return Err(IntoColorError::IntConversion);
|
||||
}
|
||||
}
|
||||
Ok(color)
|
||||
}
|
||||
|
||||
// Tuple implementation
|
||||
impl TryFrom<(i16, i16, i16)> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||
into_color(vec![tuple.0, tuple.1, tuple.2])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,6 +63,7 @@ impl TryFrom<(i16, i16, i16)> for Color {
|
|||
impl TryFrom<[i16; 3]> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
|
||||
into_color(arr.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,6 +71,7 @@ impl TryFrom<[i16; 3]> for Color {
|
|||
impl TryFrom<&[i16]> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||
into_color(slice.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,11 +6,9 @@
|
|||
// and returns the proper type.
|
||||
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn average(values: &[f64]) -> f64 {
|
||||
let total = values.iter().sum::<f64>();
|
||||
total / values.len()
|
||||
total / values.len() as f64
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -20,7 +20,6 @@ use std::error;
|
|||
use std::fmt;
|
||||
use std::num::ParseIntError;
|
||||
|
||||
// TODO: update the return type of `main()` to make this compile.
|
||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
||||
let pretend_user_input = "42";
|
||||
let x: i64 = pretend_user_input.parse()?;
|
||||
|
|
|
@ -13,12 +13,11 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
fn fruit_basket() -> HashMap<String, u32> {
|
||||
let mut basket = HashMap::new(); // TODO: declare your hash map here.
|
||||
let mut basket = HashMap::new();
|
||||
|
||||
// Two bananas are already given for you :)
|
||||
basket.insert(String::from("banana"), 2);
|
||||
|
||||
// TODO: Put more fruits in your basket here.
|
||||
basket.insert(String::from("avocado"), 2);
|
||||
basket.insert(String::from("tomato"), 1);
|
||||
basket.insert(String::from("potato"), 1);
|
||||
|
|
|
@ -32,9 +32,6 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
|||
];
|
||||
|
||||
for fruit in fruit_kinds {
|
||||
// TODO: Put new fruits if not already present. Note that you
|
||||
// are not allowed to put any type of fruit that's already
|
||||
// present!
|
||||
basket.entry(fruit).or_insert(3);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,11 +33,6 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
|||
let team_1_score: u8 = v[2].parse().unwrap();
|
||||
let team_2_name = v[1].to_string();
|
||||
let team_2_score: u8 = v[3].parse().unwrap();
|
||||
// TODO: Populate the scores table with details extracted from the
|
||||
// current line. Keep in mind that goals scored by team_1
|
||||
// will be the number of goals conceded from team_2, and similarly
|
||||
// goals scored by team_2 will be the number of goals conceded by
|
||||
// team_1.
|
||||
let team_1 = scores.entry(team_1_name.clone()).or_insert(Team {
|
||||
name: team_1_name,
|
||||
goals_scored: 0,
|
||||
|
|
|
@ -11,12 +11,12 @@
|
|||
fn main () {
|
||||
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||
|
||||
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
|
||||
let mut my_iterable_fav_fruits = my_fav_fruits.iter();
|
||||
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple"));
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach"));
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
||||
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
|
||||
assert_eq!(my_iterable_fav_fruits.next(), None);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
mod delicious_snacks {
|
||||
// TODO: Fix these use statements
|
||||
pub use self::fruits::PEAR as fruit;
|
||||
pub use self::veggies::CUCUMBER as veggie;
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
// from the std::time module. Bonus style points if you can do it with one line!
|
||||
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// TODO: Complete this use statement
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
fn main() {
|
||||
|
|
Loading…
Reference in New Issue