How to add two stdin input numbers in Rust

2 Answers

0 votes
use std::io;

fn main() {
    let mut s = String::new();
    
    // Stdin Inputs: 9 17
    io::stdin().read_line(&mut s).expect("read from stdin");

    let mut sum: i64 = 0;
    for n in s.split_whitespace() {
        sum += n.parse::<i64>().expect("Not an integer");
    }
    
    println!("{}", sum);
}





/*
run:
 
26
 
*/

 



answered Oct 20, 2022 by avibootz
0 votes
use std::io;

fn main() {
    let mut s = String::new();
    
    // Stdin Inputs: 9 17
    io::stdin().read_line(&mut s).expect("read from stdin");

    let sum: i64 = s.split_whitespace()
                    .map(|n| n.parse::<i64>().expect("Not an integer"))
                    .sum(); 
    
    println!("{}", sum);
}





/*
run:
 
26
 
*/

 



answered Oct 20, 2022 by avibootz

Related questions

1 answer 145 views
145 views asked Apr 15, 2023 by avibootz
1 answer 177 views
177 views asked Oct 20, 2022 by avibootz
1 answer 154 views
1 answer 163 views
1 answer 144 views
1 answer 165 views
165 views asked Oct 19, 2022 by avibootz
1 answer 118 views
118 views asked Oct 19, 2022 by avibootz
...