mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-01 00:09:18 +00:00
✨add new exercise
This commit is contained in:
parent
0847b3a4bf
commit
1767f47bc6
@ -100,6 +100,8 @@ bin = [
|
|||||||
{ name = "options2_sol", path = "../solutions/12_options/options2.rs" },
|
{ name = "options2_sol", path = "../solutions/12_options/options2.rs" },
|
||||||
{ name = "options3", path = "../exercises/12_options/options3.rs" },
|
{ name = "options3", path = "../exercises/12_options/options3.rs" },
|
||||||
{ name = "options3_sol", path = "../solutions/12_options/options3.rs" },
|
{ name = "options3_sol", path = "../solutions/12_options/options3.rs" },
|
||||||
|
{ name = "options4", path = "../exercises/12_options/options4.rs" },
|
||||||
|
{ name = "options4_sol", path = "../solutions/12_options/options4.rs" },
|
||||||
{ name = "errors1", path = "../exercises/13_error_handling/errors1.rs" },
|
{ name = "errors1", path = "../exercises/13_error_handling/errors1.rs" },
|
||||||
{ name = "errors1_sol", path = "../solutions/13_error_handling/errors1.rs" },
|
{ name = "errors1_sol", path = "../solutions/13_error_handling/errors1.rs" },
|
||||||
{ name = "errors2", path = "../exercises/13_error_handling/errors2.rs" },
|
{ name = "errors2", path = "../exercises/13_error_handling/errors2.rs" },
|
||||||
|
|||||||
46
exercises/12_options/options4.rs
Normal file
46
exercises/12_options/options4.rs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let optional_point = Some(Point { x: 100, y: 200 });
|
||||||
|
|
||||||
|
// Exercise 1: Use ok_or to convert optional_point to a Result.
|
||||||
|
// If the point exists, print its coordinates.
|
||||||
|
// If it doesn't, print the error message "Point does not exist".
|
||||||
|
// TODO: Fix the match statement to handle Result properly.
|
||||||
|
|
||||||
|
let result_point = optional_point.ok_or(???);
|
||||||
|
match result_point {
|
||||||
|
Ok(p) => println!("Co-ordinates are {},{}", p.x, p.y),
|
||||||
|
Err(e) => println!("{}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exercise 2: Use ok_or_else to achieve the same functionality as above,
|
||||||
|
// but with a closure to generate the error message.
|
||||||
|
// TODO: Fix the match statement to handle Result properly.
|
||||||
|
let result_point_else = optional_point.ok_or_else(???);
|
||||||
|
match result_point_else {
|
||||||
|
Ok(p) => println!("Co-ordinates are {},{}", p.x, p.y),
|
||||||
|
Err(e) => println!("{}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exercise 3: Use and_then to chain operations on optional_point.
|
||||||
|
// If the point exists, compute the distance from the origin and return it as an Option.
|
||||||
|
// If it doesn't, return None.
|
||||||
|
let distance_from_origin = optional_point.and_then(
|
||||||
|
???{
|
||||||
|
let distance = ((p.x.pow(2) + p.y.pow(2)) as f64).sqrt();
|
||||||
|
Some(distance)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
// TODO: Fix the match statement to handle Option properly.
|
||||||
|
match distance_from_origin {
|
||||||
|
Some(distance) => println!("Distance from origin is {:.2}", distance),
|
||||||
|
None => println!("No point to calculate distance"),
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{optional_point:?}"); // Don't change this line.
|
||||||
|
}
|
||||||
@ -627,6 +627,25 @@ After making the correction as suggested by the compiler, read the related docs
|
|||||||
page:
|
page:
|
||||||
https://doc.rust-lang.org/std/keyword.ref.html"""
|
https://doc.rust-lang.org/std/keyword.ref.html"""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "options4"
|
||||||
|
dir = "12_options"
|
||||||
|
test = false
|
||||||
|
hint = """
|
||||||
|
Exercise 1:
|
||||||
|
|
||||||
|
Hint: Use the ok_or method to convert the Option<Point> to a Result<Point, &str>. Handle the Ok case to print the coordinates and the Err case to print the error message.
|
||||||
|
Example: optional_point.ok_or("Point does not exist")
|
||||||
|
Exercise 2:
|
||||||
|
|
||||||
|
Hint: Use the ok_or_else method to convert the Option<Point> to a Result<Point, &str>, similar to ok_or but using a closure to generate the error message.
|
||||||
|
Example: optional_point.ok_or_else(|| "Point does not exist")
|
||||||
|
Exercise 3:
|
||||||
|
|
||||||
|
Hint: Use the and_then method to chain the operation of calculating the distance from the origin. This method should only execute if optional_point is Some.
|
||||||
|
Example: optional_point.and_then(|p| { let distance = ((p.x.pow(2) + p.y.pow(2)) as f64).sqrt(); Some(distance) })
|
||||||
|
"""
|
||||||
|
|
||||||
# ERROR HANDLING
|
# ERROR HANDLING
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
|||||||
40
solutions/12_options/options4.rs
Normal file
40
solutions/12_options/options4.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let optional_point = Some(Point { x: 100, y: 200 });
|
||||||
|
|
||||||
|
// Exercise 1: Use ok_or to convert optional_point to a Result.
|
||||||
|
// If the point exists, print its coordinates.
|
||||||
|
// If it doesn't, print the error message "Point does not exist".
|
||||||
|
let result_point = optional_point.ok_or("Point does not exist");
|
||||||
|
match result_point {
|
||||||
|
Ok(p) => println!("Co-ordinates are {},{}", p.x, p.y),
|
||||||
|
Err(e) => println!("{}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exercise 2: Use ok_or_else to achieve the same functionality as above,
|
||||||
|
// but with a closure to generate the error message.
|
||||||
|
let result_point_else = optional_point.ok_or_else(|| "Point does not exist");
|
||||||
|
match result_point_else {
|
||||||
|
Ok(p) => println!("Co-ordinates are {},{}", p.x, p.y),
|
||||||
|
Err(e) => println!("{}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exercise 3: Use and_then to chain operations on optional_point.
|
||||||
|
// If the point exists, compute the distance from the origin and return it as an Option.
|
||||||
|
// If it doesn't, return None.
|
||||||
|
let distance_from_origin = optional_point.and_then(|p| {
|
||||||
|
let distance = ((p.x.pow(2) + p.y.pow(2)) as f64).sqrt();
|
||||||
|
Some(distance)
|
||||||
|
});
|
||||||
|
match distance_from_origin {
|
||||||
|
Some(distance) => println!("Distance from origin is {:.2}", distance),
|
||||||
|
None => println!("No point to calculate distance"),
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{optional_point:?}"); // Don't change this line.
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user