Created
October 16, 2024 19:54
-
-
Save dacr/03ad9eafdc839777028bbf07eaf5491e to your computer and use it in GitHub Desktop.
hello rust errors / published by https://github.com/dacr/code-examples-manager #6bad24cb-6378-4c5d-9aad-9812eca90e86/7f160d5ae7b46c32f4e2a7140f46de27807b4d82
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rust-script | |
// summary : hello rust errors | |
// keywords : rust, errors, tries, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 6bad24cb-6378-4c5d-9aad-9812eca90e86 | |
// created-on : 2024-10-16T09:56:34+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : ./$file | |
fn divideNaive(n: u8, d: u8) -> u8 { | |
if d == 0 { | |
panic!("Can't divide by 0") | |
} | |
n / d | |
} | |
fn divide(n: u8, d: u8) -> Result<u8, String> { | |
if d == 0 { | |
return Err(String::from("Can't divide by 0")); | |
} | |
Ok(n / d) | |
} | |
fn main() { | |
let x = divide(42, 0); | |
match x { | |
Ok(n) => println!("x = {}", n), | |
Err(e) => println!("Error = {}", e), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment