nandi/rustnimpublic Fork 0
af6e50f646055dc5b291c9e602bc9ee01874f9d6
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

019-result.rs · 25 lines · 558 BRust Blame HistoryRaw
Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 14h ago1// Result-returning functions, Ok/Err construction, and matching on both.
2#[derive(Debug, PartialEq)]
3enum Error {
4 TooBig,
5}
6
7fn halve(n: i32) -> Result<i32, Error> {
8 if n > 100 {
9 Err(Error::TooBig)
10 } else {
11 Ok(n / 2)
12 }
13}
14
15fn main() {
16 for n in [10, 200] {
17 match halve(n) {
18 Ok(v) => println!("ok {}", v),
19 Err(e) => println!("err {:?}", e),
20 }
21 }
22 println!("{:?}", halve(8));
23 println!("{}", halve(8).unwrap());
24 println!("{} {}", halve(8).is_ok(), halve(500).is_err());
25}