Last active
February 3, 2026 20:24
-
-
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/4ea2f3e3b08caa6509b925044058c233661cabd1
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 License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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