Skip to content

Commit cccdc6c

Browse files
committed
solve 19 tests
1 parent 3616cfd commit cccdc6c

File tree

19 files changed

+98
-69
lines changed

19 files changed

+98
-69
lines changed

exercises/clippy/clippy1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a
1010
// hint.
1111

12-
// I AM NOT DONE
13-
1412
use std::f32;
1513

1614
fn main() {
17-
let pi = 3.14f32;
15+
let pi = f32::consts::PI;
1816
let radius = 5.00f32;
1917

2018
let area = pi * f32::powi(radius, 2);

exercises/clippy/clippy2.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// clippy2.rs
2-
//
2+
//
33
// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
fn main() {
97
let mut res = 42;
108
let option = Some(12);
11-
for x in option {
9+
while let Some(x) = option {
1210
res += x;
1311
}
1412
println!("{}", res);

exercises/clippy/clippy3.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
// clippy3.rs
2-
//
2+
//
33
// Here's a couple more easy Clippy fixes, so you can see its utility.
44
//
55
// Execute `rustlings hint clippy3` or use the `hint` watch subcommand for a hint.
66

7-
// I AM NOT DONE
8-
97
#[allow(unused_variables, unused_assignments)]
108
fn main() {
119
let my_option: Option<()> = None;
12-
if my_option.is_none() {
13-
my_option.unwrap();
14-
}
1510

11+
#[rustfmt::skip]
1612
let my_arr = &[
17-
-1, -2, -3
13+
-1, -2, -3,
1814
-4, -5, -6
1915
];
2016
println!("My array! Here it is: {:?}", my_arr);
2117

22-
let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5);
18+
let mut my_empty_vec = vec![1, 2, 3, 4, 5];
19+
my_empty_vec.clear();
2320
println!("This Vec is empty, see? {:?}", my_empty_vec);
2421

2522
let mut value_a = 45;
2623
let mut value_b = 66;
2724
// Let's swap these two!
28-
value_a = value_b;
29-
value_b = value_a;
25+
std::mem::swap(&mut value_a, &mut value_b);
3026
println!("value a: {}; value b: {}", value_a, value_b);
3127
}

exercises/conversions/as_ref_mut.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,23 @@
77
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
1210
// Obtain the number of bytes (not characters) in the given argument.
1311
// TODO: Add the AsRef trait appropriately as a trait bound.
14-
fn byte_counter<T>(arg: T) -> usize {
12+
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
1513
arg.as_ref().as_bytes().len()
1614
}
1715

1816
// Obtain the number of characters (not bytes) in the given argument.
1917
// TODO: Add the AsRef trait appropriately as a trait bound.
20-
fn char_counter<T>(arg: T) -> usize {
18+
fn char_counter<T: AsRef<str>>(arg: T) -> usize {
2119
arg.as_ref().chars().count()
2220
}
2321

2422
// Squares a number using as_mut().
2523
// TODO: Add the appropriate trait bound.
26-
fn num_sq<T>(arg: &mut T) {
24+
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
2725
// TODO: Implement the function body.
28-
???
26+
*arg.as_mut() *= *arg.as_mut()
2927
}
3028

3129
#[cfg(test)]

exercises/conversions/from_into.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,27 @@ impl Default for Person {
4040
// If while parsing the age, something goes wrong, then return the default of
4141
// Person Otherwise, then return an instantiated Person object with the results
4242

43-
// I AM NOT DONE
44-
4543
impl From<&str> for Person {
4644
fn from(s: &str) -> Person {
45+
let mut ret = Person::default();
46+
if s.len() == 0 {
47+
return ret;
48+
}
49+
let items: Vec<&str> = s.split(",").collect();
50+
if items.len()!=2 {
51+
return ret;
52+
}
53+
let name = items[0].to_string();
54+
if name.len() == 0 {
55+
return ret;
56+
}
57+
let age_result = items[1].parse::<usize>();
58+
match age_result {
59+
Ok(age) => ret.age = age,
60+
Err(_) => return ret,
61+
}
62+
ret.name = name;
63+
ret
4764
}
4865
}
4966

exercises/conversions/from_str.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ enum ParsePersonError {
3131
ParseInt(ParseIntError),
3232
}
3333

34-
// I AM NOT DONE
35-
3634
// Steps:
3735
// 1. If the length of the provided string is 0, an error should be returned
3836
// 2. Split the given string on the commas present in it
@@ -52,6 +50,27 @@ enum ParsePersonError {
5250
impl FromStr for Person {
5351
type Err = ParsePersonError;
5452
fn from_str(s: &str) -> Result<Person, Self::Err> {
53+
if s.len() == 0 {
54+
return Err(ParsePersonError::Empty);
55+
}
56+
let items: Vec<&str> = s.split(",").collect();
57+
if items.len() != 2 {
58+
return Err(ParsePersonError::BadLen);
59+
}
60+
let name = items[0].to_string();
61+
if name.len() == 0 {
62+
return Err(ParsePersonError::NoName);
63+
}
64+
let age_result = items[1].parse::<usize>();
65+
let mut age: usize = 0;
66+
match age_result {
67+
Ok(x) => age = x,
68+
Err(x) => return Err(ParsePersonError::ParseInt(x)),
69+
}
70+
Ok(Person {
71+
name: name,
72+
age: age,
73+
})
5574
}
5675
}
5776

exercises/conversions/try_from_into.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ enum IntoColorError {
2727
IntConversion,
2828
}
2929

30-
// I AM NOT DONE
31-
3230
// Your task is to complete this implementation and return an Ok result of inner
3331
// type Color. You need to create an implementation for a tuple of three
3432
// integers, an array of three integers, and a slice of integers.
@@ -41,20 +39,36 @@ enum IntoColorError {
4139
impl TryFrom<(i16, i16, i16)> for Color {
4240
type Error = IntoColorError;
4341
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
42+
for i in [tuple.0, tuple.1, tuple.2] {
43+
if i < 0 || i > 255 {
44+
return Err(IntoColorError::IntConversion);
45+
}
46+
}
47+
return Ok(Color {
48+
red: tuple.0 as u8,
49+
green: tuple.1 as u8,
50+
blue: tuple.2 as u8,
51+
});
4452
}
4553
}
4654

4755
// Array implementation
4856
impl TryFrom<[i16; 3]> for Color {
4957
type Error = IntoColorError;
5058
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
59+
Color::try_from((arr[0], arr[1], arr[2]))
5160
}
5261
}
5362

5463
// Slice implementation
5564
impl TryFrom<&[i16]> for Color {
5665
type Error = IntoColorError;
5766
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
67+
if slice.len() != 3 {
68+
return Err(IntoColorError::BadLen);
69+
} else {
70+
return Color::try_from((slice[0], slice[1], slice[2]));
71+
}
5872
}
5973
}
6074

exercises/conversions/using_as.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a
1111
// hint.
1212

13-
// I AM NOT DONE
14-
1513
fn average(values: &[f64]) -> f64 {
1614
let total = values.iter().sum::<f64>();
17-
total / values.len()
15+
total / values.len() as f64
1816
}
1917

2018
fn main() {

exercises/macros/macros1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
86
macro_rules! my_macro {
97
() => {
108
println!("Check out my macro!");
119
};
1210
}
1311

1412
fn main() {
15-
my_macro();
13+
my_macro!();
1614
}

exercises/macros/macros2.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a
44
// hint.
55

6-
// I AM NOT DONE
7-
8-
fn main() {
9-
my_macro!();
10-
}
11-
126
macro_rules! my_macro {
137
() => {
148
println!("Check out my macro!");
159
};
1610
}
11+
12+
fn main() {
13+
my_macro!();
14+
}

0 commit comments

Comments
 (0)