Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7. Error Handling using Pattern Matching #7

Open
prophen opened this issue Jan 23, 2020 · 2 comments
Open

7. Error Handling using Pattern Matching #7

prophen opened this issue Jan 23, 2020 · 2 comments

Comments

@prophen
Copy link
Owner

prophen commented Jan 23, 2020

https://github.com/prophen/intro-to-rust/blob/nikema's-notes/exercises/07.md

@prophen
Copy link
Owner Author

prophen commented Jan 23, 2020

use std::io;

fn main() {
    println!("Please enter the first number: ");
    let mut first = String::new();
    io::stdin().read_line(&mut first).unwrap();
    let mut a: u32 = 0;
    match first.trim().parse() {
        Ok(value) => a = value,
        Err(_error)=> eprintln!("This is unfortunately not a number")
    };

    println!("Please enter the second number: ");
    let mut second = String::new();
    io::stdin().read_line(&mut second).unwrap();
    let mut b: u32 = 0;
    match second.trim().parse() {
        Ok(value) => b = value,
        Err(_error)=> eprintln!("This is unfortunately not a number")
    };

    let result = sum(a, b);
    println!("Result: {}", result);
}
fn sum(a: u32, b:u32 ) -> u32 {
    a + b
}

image

@prophen
Copy link
Owner Author

prophen commented Jan 23, 2020

next issue #8
Go back to wiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant