Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Option type
- fn safe_divide(dividend: i64, divisor: i64) -> Option<i64> {
- if 0 == divisor {
- None
- } else {
- Some(dividend / divisor)
- }
- }
- fn main() {
- let x = 10;
- let y = 5;
- match safe_divide(x, y) {
- Some(quotient) => println!("{} / {} = {}", x, y, quotient),
- None => panic!("the disco!"),
- }
- let y = 0;
- match safe_divide(x, y) {
- Some(quotient) => println!("{} / {} = {}", x, y, quotient),
- _otherwise => panic!("the disco!"),
- }
- }
Add Comment
Please, Sign In to add comment