I'm trying to wrap my head around errors, results, match, ?, and so forth. The code seems to work, mostly, but I feel like I'm missing something.
Not sure why I need to n.unwrap() on line 30 in order to println! on line 31.
Any feedback would be appreciated.
Code is at rust_error_handling/main.rs at main · beephsupreme/rust_error_handling · GitHub
H2CO3
September 28, 2022, 9:08pm
2
Because n
is the return value of get_int()
, which has type Result<usize, MyError>
. Your match is a no-op in the Ok case, since you throw away its value. You probably wanted to assign it to a variable.
1 Like
E.g.
fn main() {
let n = match get_int() {
Ok(n) => n,
Err(e) => panic!("{}: {}", e, "Could not get n"),
};
println!("{}", n);
}
match
doesn't modify things in place. But it is an expression that can "return" a value for you to assign.
system
Closed
December 27, 2022, 9:14pm
4
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.