nandi/rustnimpublic Fork 0
afb2a6e152356d5a78d536ad75f005bb1fc7ed63
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.

020-question-mark.rs · 24 lines · 437 BRust Blame HistoryRaw
Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 18h ago1// `?` expands to an early return on the error branch.
2#[derive(Debug)]
3enum Error {
4 Odd,
5}
6
7fn halve(n: i32) -> Result<i32, Error> {
8 if n % 2 != 0 {
9 return Err(Error::Odd);
10 }
11 Ok(n / 2)
12}
13
14fn quarter(n: i32) -> Result<i32, Error> {
15 let half = halve(n)?;
16 let q = halve(half)?;
17 Ok(q)
18}
19
20fn main() {
21 println!("{:?}", quarter(8));
22 println!("{:?}", quarter(6));
23 println!("{:?}", quarter(5));
24}