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

10. Making the code more DRY #10

Open
prophen opened this issue Jan 23, 2020 · 1 comment
Open

10. Making the code more DRY #10

prophen opened this issue Jan 23, 2020 · 1 comment

Comments

@prophen
Copy link
Owner

prophen commented Jan 23, 2020

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

@prophen
Copy link
Owner Author

prophen commented Jan 23, 2020

use std::io;
use std::process;

fn main() {
    loop {
        println!("Please enter the first number: ");
        let a: u32 = read_user_data();
        println!("Please enter the second number: ");
        let b: u32 = read_user_data();
        let result = sum(a, b);
        println!("Result: {}", result);
    }
}
fn sum(a: u32, b:u32 ) -> u32 {
    a + b
}
fn read_user_data() -> u32 {
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
    let number: u32;
    match input.trim().parse() {
        Ok(value) => number = value,
        Err(_error)=> {
            eprintln!("This is unfortunately not a number");
            process::exit(1)
        }
    }
    number
}

image

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