When I read chapter18.1 of the book, I found that I couldn't understand this piece of code in Listing 18-1:
let age: Result<u8, _> = "34".parse();
It converts "34"
into Result::Ok(u8)
, but if the string literal can't convert into Ok(u8)
, what Error type it will convert into? What is the purpose of a underscore pattern in the angle brackets of generic types? How can compiler confirm the type of underscore?
This is my try:
fn main() {
let age: Result<u8, _> = “abc114514”.parse();
println(“{:?}”, age);
}
Program running results:
Err(ParseIntError { kind: InvalidDigit })