Examples · learn/07-errors · 129 of 339
question
tin run examples/learn/07-errors/question.tinfn parsePort(text: string): Result<int, string> {
match parseInt(text) {
Some(n) => if n > 0 && n < 65536 { Ok(n) } else { Err("port {n} is out of range") }
None => Err("'{text}' is not a number")
}
}
// ? takes the Ok value out, or returns the Err from this function right away.
fn address(host: string, port: string): Result<string, string> {
let p = parsePort(port)?
Ok("{host}:{p}")
}
fn main() {
print(address("localhost", "8080"))
print(address("localhost", "http"))
print(address("localhost", "99999"))
}