Skip to content

Commit 0c85dc1

Browse files
committed
feat: Add type conversion and parsing exercises
1 parent fe10e06 commit 0c85dc1

File tree

7 files changed

+302
-0
lines changed

7 files changed

+302
-0
lines changed

exercises/conversions/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### Type conversions
2+
3+
4+
Rust offers a multitude of ways to convert a value of a given type into another type.
5+
6+
The simplest form of type conversion is a type cast expression. It is denoted with the binary operator `as`. For instance, `println!("{}", 1 + 1.0);` would not compile, since `1` is an integer while `1.0` is a float. However, `println!("{}", 1 as f32 + 1.0)` should compile. The exercise [`using_as`](using_as.rs) tries to cover this.
7+
8+
Rust also offers traits that facilitate type conversions upon implementation. These traits can be found under the [`convert`](https://doc.rust-lang.org/std/convert/index.html) module.
9+
The traits are the following:
10+
- `From` and `Into` covered in [`from_into`](from_into.rs)
11+
- `TryFrom` and `TryInto` covered in [`try_from_into`](try_from_into.rs)
12+
- `AsRef` and `AsMut` covered in [`as_ref_mut`](as_ref_mut.rs)
13+
14+
Furthermore, the `std::str` module offers a trait called [`FromStr`](https://doc.rust-lang.org/std/str/trait.FromStr.html) which helps with converting strings into target types via the `parse` method on strings. If properly implemented for a given type `Person`, then `let p: Person = "Mark,20".parse().unwrap()` should both compile and run without panicking.
15+
16+
These should be the main ways ***within the standard library*** to convert data into your desired types.
17+
18+
#### Book Sections
19+
20+
These are not directly covered in the book, but the standard library has great documentation for [conversions here](https://doc.rust-lang.org/std/convert/index.html). The `FromStr` trait is also covered [here](https://doc.rust-lang.org/std/str/trait.FromStr.html).

exercises/conversions/as_ref_mut.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// AsRef and AsMut allow for cheap reference-to-reference conversions.
2+
// Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html
3+
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
4+
5+
// Obtain the number of bytes (not characters) in the given argument
6+
// Add the AsRef trait appropriately as a trait bound
7+
fn byte_counter<T>(arg: T) -> usize {
8+
arg.as_ref().as_bytes().len()
9+
}
10+
11+
// Obtain the number of characters (not bytes) in the given argument
12+
// Add the AsRef trait appropriately as a trait bound
13+
fn char_counter<T>(arg: T) -> usize {
14+
arg.as_ref().chars().collect::<Vec<_>>().len()
15+
}
16+
17+
fn main() {
18+
let s = "Café au lait";
19+
println!("{}", char_counter(s));
20+
println!("{}", byte_counter(s));
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn different_counts() {
29+
let s = "Café au lait";
30+
assert_ne!(char_counter(s), byte_counter(s));
31+
}
32+
fn same_counts() {
33+
let s = "Cafe au lait";
34+
assert_eq!(char_counter(s), byte_counter(s));
35+
}
36+
}

exercises/conversions/from_into.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// The From trait is used for value-to-value conversions.
2+
// If From is implemented correctly for a type, the Into trait should work conversely.
3+
// You can read more about it at https://doc.rust-lang.org/std/convert/trait.From.html
4+
#[derive(Debug)]
5+
struct Person {
6+
name: String,
7+
age: usize,
8+
}
9+
10+
// We implement the Default trait to use it as a fallback
11+
// when the provided string is not convertible into a Person object
12+
impl Default for Person {
13+
fn default() -> Person {
14+
Person {
15+
name: String::from("John"),
16+
age: 30,
17+
}
18+
}
19+
}
20+
21+
// Your task is to complete this implementation
22+
// in order for the line `let p = Person::from("Mark,20")` to compile
23+
// Please note that you'll need to parse the age component into a `usize`
24+
// with something like `"4".parse::<usize>()`. The outcome of this needs to
25+
// be handled appropriately.
26+
//
27+
// Steps:
28+
// 1. If the length of the provided string is 0, then return the default of Person
29+
// 2. Split the given string on the commas present in it
30+
// 3. Extract the first element from the split operation and use it as the name
31+
// 4. Extract the other element from the split operation and parse it into a `usize` as the age
32+
// If while parsing the age, something goes wrong, then return the default of Person
33+
// Otherwise, then return an instantiated Person onject with the results
34+
impl From<&str> for Person {
35+
fn from(s: &str) -> Person {
36+
}
37+
}
38+
39+
fn main() {
40+
// Use the `from` function
41+
let p1 = Person::from("Mark,20");
42+
// Since From is implemented for Person, we should be able to use Into
43+
let p2: Person = "Gerald,70".into();
44+
println!("{:?}", p1);
45+
println!("{:?}", p2);
46+
}
47+
48+
#[cfg(test)]
49+
mod tests {
50+
use super::*;
51+
#[test]
52+
fn test_default() {
53+
// Test that the default person is 30 year old John
54+
let dp = Person::default();
55+
assert_eq!(dp.name, "John");
56+
assert_eq!(dp.age, 30);
57+
}
58+
#[test]
59+
fn test_bad_convert() {
60+
// Test that John is returned when bad string is provided
61+
let p = Person::from("");
62+
assert_eq!(p.name, "John");
63+
assert_eq!(p.age, 30);
64+
}
65+
#[test]
66+
fn test_good_convert() {
67+
// Test that "Mark,20" works
68+
let p = Person::from("Mark,20");
69+
assert_eq!(p.name, "Mark");
70+
assert_eq!(p.age, 20);
71+
}
72+
}

exercises/conversions/from_str.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This does practically the same thing that TryFrom<&str> does.
2+
// Additionally, upon implementing FromStr, you can use the `parse` method
3+
// on strings to generate an object of the implementor type.
4+
// You can read more about it at https://doc.rust-lang.org/std/str/trait.FromStr.html
5+
use std::str::FromStr;
6+
7+
#[derive(Debug)]
8+
struct Person {
9+
name: String,
10+
age: usize,
11+
}
12+
13+
// Steps:
14+
// 1. If the length of the provided string is 0, then return an error
15+
// 2. Split the given string on the commas present in it
16+
// 3. Extract the first element from the split operation and use it as the name
17+
// 4. Extract the other element from the split operation and parse it into a `usize` as the age
18+
// If while parsing the age, something goes wrong, then return an error
19+
// Otherwise, then return a Result of a Person object
20+
impl FromStr for Person {
21+
type Err = String;
22+
fn from_str(s: &str) -> Result<Person, Self::Err> {
23+
}
24+
}
25+
26+
fn main() {
27+
let p = "Mark,20".parse::<Person>().unwrap();
28+
println!("{:?}", p);
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
35+
#[test]
36+
fn empty_input() {
37+
assert!("".parse::<Person>().is_err());
38+
}
39+
#[test]
40+
fn good_input() {
41+
assert!("John,32".parse::<Person>().is_ok());
42+
}
43+
#[test]
44+
#[should_panic]
45+
fn missing_age() {
46+
"John".parse::<Person>().unwrap();
47+
}
48+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// TryFrom is a simple and safe type conversion that may fail in a controlled way under some circumstances.
2+
// Basically, this is the same as From. The main difference is that this should return a Result type
3+
// instead of the target type itself.
4+
// You can read more about it at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
5+
use std::convert::{TryInto, TryFrom};
6+
7+
#[derive(Debug)]
8+
struct Person {
9+
name: String,
10+
age: usize,
11+
}
12+
13+
// Your task is to complete this implementation
14+
// in order for the line `let p = Person::try_from("Mark,20")` to compile
15+
// and return an Ok result of inner type Person.
16+
// Please note that you'll need to parse the age component into a `usize`
17+
// with something like `"4".parse::<usize>()`. The outcome of this needs to
18+
// be handled appropriately.
19+
//
20+
// Steps:
21+
// 1. If the length of the provided string is 0, then return an error
22+
// 2. Split the given string on the commas present in it
23+
// 3. Extract the first element from the split operation and use it as the name
24+
// 4. Extract the other element from the split operation and parse it into a `usize` as the age
25+
// If while parsing the age, something goes wrong, then return an error
26+
// Otherwise, then return a Result of a Person object
27+
impl TryFrom<&str> for Person {
28+
type Error = String;
29+
fn try_from(s: &str) -> Result<Self, Self::Error> {
30+
}
31+
}
32+
33+
fn main() {
34+
// Use the `from` function
35+
let p1 = Person::try_from("Mark,20");
36+
// Since From is implemented for Person, we should be able to use Into
37+
let p2: Result<Person, _> = "Gerald,70".try_into();
38+
println!("{:?}", p1);
39+
println!("{:?}", p2);
40+
}
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
#[test]
46+
fn test_bad_convert() {
47+
// Test that John is returned when bad string is provided
48+
let p = Person::try_from("");
49+
assert!(p.is_err());
50+
}
51+
#[test]
52+
fn test_good_convert() {
53+
// Test that "Mark,20" works
54+
let p = Person::try_from("Mark,20");
55+
assert!(p.is_ok());
56+
let p = p.unwrap();
57+
assert_eq!(p.name, "Mark");
58+
assert_eq!(p.age, 20);
59+
}
60+
#[test]
61+
#[should_panic]
62+
fn test_panic_empty_input() {
63+
let p: Person = "".try_into().unwrap();
64+
}
65+
#[test]
66+
#[should_panic]
67+
fn test_panic_bad_age() {
68+
let p = Person::try_from("Mark,twenty").unwrap();
69+
}
70+
}

exercises/conversions/using_as.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Type casting in Rust is done via the usage of the `as` operator.
2+
// Please note that the `as` operator is not only used when type casting.
3+
// It also helps with renaming imports.
4+
5+
// The goal is to make sure that the division does not fail to compile
6+
fn average(values: &[f64]) -> f64 {
7+
let total = values
8+
.iter()
9+
.fold(0.0, |a, b| a + b);
10+
total / values.len()
11+
}
12+
13+
fn main() {
14+
let values = [3.5, 0.3, 13.0, 11.7];
15+
println!("{}", average(&values));
16+
}

info.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,3 +610,43 @@ answers and don't understand why they work and yours doesn't.
610610
If you've learned from the sample solutions, I encourage you to come
611611
back to this exercise and try it again in a few days to reinforce
612612
what you've learned :)"""
613+
614+
# TYPE CONVERSIONS
615+
616+
[[exercises]]
617+
name = "using_as"
618+
path = "exercises/conversions/using_as.rs"
619+
mode = "compile"
620+
hint = """
621+
Use the `as` operator to cast one of the operands in the last line of the
622+
`average` function into the expected return type."""
623+
624+
[[exercises]]
625+
name = "from_into"
626+
path = "exercises/conversions/from_into.rs"
627+
mode = "test"
628+
hint = """
629+
Follow the steps provided right before the `From` implementation"""
630+
631+
[[exercises]]
632+
name = "try_from_into"
633+
path = "exercises/conversions/try_from_into.rs"
634+
mode = "test"
635+
hint = """
636+
Follow the steps provided right before the `From` implementation.
637+
You can also use the example at https://doc.rust-lang.org/std/convert/trait.TryFrom.html"""
638+
639+
[[exercises]]
640+
name = "as_ref_mut"
641+
path = "exercises/conversions/as_ref_mut.rs"
642+
mode = "test"
643+
hint = """
644+
Add AsRef<str> as a trait bound to the functions."""
645+
646+
[[exercises]]
647+
name = "from_str"
648+
path = "exercises/conversions/from_str.rs"
649+
mode = "test"
650+
hint = """
651+
If you've already solved try_from_into.rs, then this is almost a copy-paste.
652+
Otherwise, go ahead and solve try_from_into.rs first."""

0 commit comments

Comments
 (0)