Skip to content

Commit 699dbed

Browse files
committed
update
1 parent 8e88362 commit 699dbed

File tree

11 files changed

+127
-17
lines changed

11 files changed

+127
-17
lines changed

exercises/conversions/as_ref_mut.rs

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

10-
// I AM NOT DONE
10+
1111

1212
// Obtain the number of bytes (not characters) in the given argument.
1313
// TODO: Add the AsRef trait appropriately as a trait bound.
14-
fn byte_counter<T>(arg: T) -> usize {
14+
fn byte_counter<T:AsRef<str> >(arg: T) -> usize {
1515
arg.as_ref().as_bytes().len()
1616
}
1717

1818
// Obtain the number of characters (not bytes) in the given argument.
1919
// TODO: Add the AsRef trait appropriately as a trait bound.
20-
fn char_counter<T>(arg: T) -> usize {
20+
fn char_counter<T:AsRef<str>>(arg: T) -> usize {
2121
arg.as_ref().chars().count()
2222
}
2323

2424
// Squares a number using as_mut().
2525
// TODO: Add the appropriate trait bound.
26-
fn num_sq<T>(arg: &mut T) {
26+
fn num_sq<T:AsMut<u32>>(arg: &mut T) {
2727
// TODO: Implement the function body.
28-
???
28+
let num : &mut u32 = arg.as_mut();
29+
*num = *num * *num;
2930
}
3031

3132
#[cfg(test)]

exercises/conversions/from_str.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum ParsePersonError {
3131
ParseInt(ParseIntError),
3232
}
3333

34-
// I AM NOT DONE
34+
3535

3636
// Steps:
3737
// 1. If the length of the provided string is 0, an error should be returned
@@ -52,6 +52,37 @@ enum ParsePersonError {
5252
impl FromStr for Person {
5353
type Err = ParsePersonError;
5454
fn from_str(s: &str) -> Result<Person, Self::Err> {
55+
if s.is_empty(){
56+
return Err(Self::Err::Empty);
57+
}
58+
59+
let parts :Vec<&str> = s.split(',').collect();
60+
if parts.len() !=2{
61+
return Err(Self::Err::BadLen);
62+
}
63+
64+
let name = match parts.get(0){
65+
Some(name) => {
66+
let n = (*name).trim();
67+
if n.is_empty(){
68+
return Err(Self::Err::NoName);
69+
}
70+
n
71+
},
72+
None => return Err(Self::Err::NoName),
73+
};
74+
75+
let age = match parts.get(1){
76+
Some(age) => match age.parse::<usize>(){
77+
Ok(age) => age,
78+
Err(e) => return Err(Self::Err::ParseInt(e)),
79+
},
80+
None => return Err(Self::Err::BadLen),
81+
};
82+
Ok(Person{
83+
name : String::from(name),
84+
age
85+
})
5586
}
5687
}
5788

exercises/conversions/try_from_into.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum IntoColorError {
2727
IntConversion,
2828
}
2929

30-
// I AM NOT DONE
30+
3131

3232
// Your task is to complete this implementation and return an Ok result of inner
3333
// type Color. You need to create an implementation for a tuple of three
@@ -41,20 +41,82 @@ enum IntoColorError {
4141
impl TryFrom<(i16, i16, i16)> for Color {
4242
type Error = IntoColorError;
4343
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
44+
let red : u8 = match tuple.0.try_into(){
45+
Ok(red) => red,
46+
Err(_) => return Err(Self::Error::IntConversion),
47+
};
48+
let green:u8 = match tuple.1.try_into(){
49+
Ok(green) => green,
50+
Err(_) => return Err(Self::Error::IntConversion),
51+
};
52+
let blue :u8 = match tuple.2.try_into(){
53+
Ok(blue) => blue,
54+
Err(_) => return Err(Self::Error::IntConversion),
55+
};
56+
Ok(
57+
Color{
58+
red,
59+
green,
60+
blue
61+
}
62+
)
63+
4464
}
4565
}
4666

4767
// Array implementation
4868
impl TryFrom<[i16; 3]> for Color {
4969
type Error = IntoColorError;
5070
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
71+
let red : u8 = match arr[0].try_into(){
72+
Ok(red) => red,
73+
Err(_) => return Err(Self::Error::IntConversion),
74+
};
75+
let green:u8 = match arr[1].try_into(){
76+
Ok(green) => green,
77+
Err(_) => return Err(Self::Error::IntConversion),
78+
};
79+
let blue :u8 = match arr[2].try_into(){
80+
Ok(blue) => blue,
81+
Err(_) => return Err(Self::Error::IntConversion),
82+
};
83+
Ok(
84+
Color{
85+
red,
86+
green,
87+
blue
88+
}
89+
)
5190
}
5291
}
5392

5493
// Slice implementation
5594
impl TryFrom<&[i16]> for Color {
5695
type Error = IntoColorError;
5796
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
97+
if slice.len() !=3{
98+
return Err(Self::Error::BadLen);
99+
}
100+
let red : u8 = match slice[0].try_into(){
101+
Ok(red) => red,
102+
Err(_) => return Err(Self::Error::IntConversion),
103+
};
104+
let green:u8 = match slice[1].try_into(){
105+
Ok(green) => green,
106+
Err(_) => return Err(Self::Error::IntConversion),
107+
};
108+
let blue :u8 = match slice[2].try_into(){
109+
Ok(blue) => blue,
110+
Err(_) => return Err(Self::Error::IntConversion),
111+
};
112+
Ok(
113+
Color{
114+
red,
115+
green,
116+
blue
117+
}
118+
)
119+
58120
}
59121
}
60122

exercises/tests/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exercises/tests/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "tests8"
3+
version = "0.0.1"
4+
edition = "2021"
5+
[[bin]]
6+
name = "tests8"
7+
path = "tests8.rs"

exercises/tests/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ fn main() {
1111
.unwrap()
1212
.as_secs(); // What's the use of this timestamp here?
1313
let your_command = format!(
14-
"Your command here with {}, please checkout exercises/tests/build.rs",
14+
"rustc-env=TEST_FOO={}",
1515
timestamp
1616
);
1717
println!("cargo:{}", your_command);
1818

1919
// In tests8, we should enable "pass" feature to make the
2020
// testcase return early. Fill in the command to tell
2121
// Cargo about that.
22-
let your_command = "Your command here, please checkout exercises/tests/build.rs";
22+
let your_command = "rustc-cfg=feature=\"pass\"";
2323
println!("cargo:{}", your_command);
2424
}

exercises/tests/tests5.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// Execute `rustlings hint tests5` or use the `hint` watch subcommand for a
2323
// hint.
2424

25-
// I AM NOT DONE
25+
2626

2727
/// # Safety
2828
///
@@ -32,7 +32,7 @@ unsafe fn modify_by_address(address: usize) {
3232
// code's behavior and the contract of this function. You may use the
3333
// comment of the test below as your format reference.
3434
unsafe {
35-
todo!("Your code goes here")
35+
*(address as * mut u32 ) = 0xAABBCCDD;
3636
}
3737
}
3838

exercises/tests/tests6.rs

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

10-
// I AM NOT DONE
1110

1211
struct Foo {
1312
a: u128,
@@ -20,8 +19,9 @@ struct Foo {
2019
unsafe fn raw_pointer_to_box(ptr: *mut Foo) -> Box<Foo> {
2120
// SAFETY: The `ptr` contains an owned box of `Foo` by contract. We
2221
// simply reconstruct the box from that pointer.
23-
let mut ret: Box<Foo> = unsafe { ??? };
24-
todo!("The rest of the code goes here")
22+
let mut ret: Box<Foo> = unsafe {Box::from_raw(ptr)};
23+
ret.b = Some(String::from("hello"));
24+
ret
2525
}
2626

2727
#[cfg(test)]

exercises/tests/tests7.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// Execute `rustlings hint tests7` or use the `hint` watch subcommand for a
3535
// hint.
3636

37-
// I AM NOT DONE
37+
3838

3939
fn main() {}
4040

exercises/tests/tests8.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Execute `rustlings hint tests8` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
10+
1111

1212
fn main() {}
1313

0 commit comments

Comments
 (0)