Skip to content

Commit ca6dc3f

Browse files
yllmisyllmis
authored andcommitted
update
1 parent d7fe8d8 commit ca6dc3f

File tree

13 files changed

+110
-53
lines changed

13 files changed

+110
-53
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: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
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
7+
use std::mem::swap;
88

99
#[allow(unused_variables, unused_assignments)]
1010
fn main() {
1111
let my_option: Option<()> = None;
12-
if my_option.is_none() {
13-
my_option.unwrap();
14-
}
12+
// if my_option.is_some() {
13+
// my_option.unwrap();
14+
// }
1515

16-
let my_arr = &[
17-
-1, -2, -3
18-
-4, -5, -6
19-
];
16+
let my_arr = &[-1, -2, -3, -4, -5, -6];
2017
println!("My array! Here it is: {:?}", my_arr);
2118

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

2523
let mut value_a = 45;
2624
let mut value_b = 66;
2725
// Let's swap these two!
28-
value_a = value_b;
29-
value_b = value_a;
26+
swap(&mut value_a, &mut value_b);
3027
println!("value a: {}; value b: {}", value_a, value_b);
3128
}

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()) * (*arg.as_mut());
2927
}
3028

3129
#[cfg(test)]

exercises/conversions/from_into.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,33 @@ 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+
if s.is_empty() {
46+
return Person::default();
47+
}
48+
49+
let parts: Vec<&str> = s.split(',').collect();
50+
51+
if parts.len() != 2 {
52+
return Person::default();
53+
}
54+
55+
let name = parts[0].trim();
56+
let age_str = parts[1].trim();
57+
58+
if name.is_empty() || age_str.is_empty() {
59+
return Person::default();
60+
}
61+
62+
if let Ok(age) = age_str.parse::<usize>() {
63+
Person {
64+
name: name.to_string(),
65+
age,
66+
}
67+
} else {
68+
Person::default()
69+
}
4770
}
4871
}
4972

exercises/conversions/from_str.rs

Lines changed: 17 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,23 @@ enum ParsePersonError {
5250
impl FromStr for Person {
5351
type Err = ParsePersonError;
5452
fn from_str(s: &str) -> Result<Person, Self::Err> {
53+
if s.is_empty() {
54+
return Err(ParsePersonError::Empty);
55+
}
56+
let parts: Vec<&str> = s.split(',').collect();
57+
if parts.len() != 2 {
58+
return Err(ParsePersonError::BadLen);
59+
}
60+
let name = parts[0].trim();
61+
if name.is_empty() {
62+
return Err(ParsePersonError::NoName);
63+
}
64+
let age_str = parts[1].trim();
65+
let age: usize = age_str.parse().map_err(ParsePersonError::ParseInt)?;
66+
Ok(Person {
67+
name: name.to_string(),
68+
age,
69+
})
5570
}
5671
}
5772

exercises/conversions/try_from_into.rs

Lines changed: 39 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,59 @@ 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+
if tuple.0 < 0
43+
|| tuple.0 > 255
44+
|| tuple.1 < 0
45+
|| tuple.1 > 255
46+
|| tuple.2 < 0
47+
|| tuple.2 > 255
48+
{
49+
return Err(IntoColorError::IntConversion);
50+
}
51+
Ok(Color {
52+
red: tuple.0 as u8,
53+
green: tuple.1 as u8,
54+
blue: tuple.2 as u8,
55+
})
4456
}
4557
}
4658

4759
// Array implementation
4860
impl TryFrom<[i16; 3]> for Color {
4961
type Error = IntoColorError;
5062
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
63+
if arr[0] < 0 || arr[0] > 255 || arr[1] < 0 || arr[1] > 255 || arr[2] < 0 || arr[2] > 255 {
64+
return Err(IntoColorError::IntConversion);
65+
}
66+
Ok(Color {
67+
red: arr[0] as u8,
68+
green: arr[1] as u8,
69+
blue: arr[2] as u8,
70+
})
5171
}
5272
}
5373

5474
// Slice implementation
5575
impl TryFrom<&[i16]> for Color {
5676
type Error = IntoColorError;
5777
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
78+
if slice.len() != 3 {
79+
return Err(IntoColorError::BadLen);
80+
}
81+
if slice[0] < 0
82+
|| slice[0] > 255
83+
|| slice[1] < 0
84+
|| slice[1] > 255
85+
|| slice[2] < 0
86+
|| slice[2] > 255
87+
{
88+
return Err(IntoColorError::IntConversion);
89+
}
90+
Ok(Color {
91+
red: slice[0] as u8,
92+
green: slice[1] as u8,
93+
blue: slice[2] as u8,
94+
})
5895
}
5996
}
6097

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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
//
33
// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a
44
// hint.
5-
6-
// I AM NOT DONE
7-
8-
fn main() {
9-
my_macro!();
10-
}
11-
125
macro_rules! my_macro {
136
() => {
147
println!("Check out my macro!");
158
};
169
}
10+
11+
fn main() {
12+
my_macro!();
13+
}

0 commit comments

Comments
 (0)