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 252 views
252 views asked Apr 15, 2023 by avibootz
1 answer 290 views
290 views asked Oct 20, 2022 by avibootz
1 answer 263 views
1 answer 274 views
1 answer 252 views
1 answer 257 views
257 views asked Oct 19, 2022 by avibootz
1 answer 209 views
209 views asked Oct 19, 2022 by avibootz
...