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 164 views
164 views asked Apr 15, 2023 by avibootz
1 answer 203 views
203 views asked Oct 20, 2022 by avibootz
1 answer 173 views
1 answer 188 views
1 answer 171 views
1 answer 182 views
182 views asked Oct 19, 2022 by avibootz
1 answer 141 views
141 views asked Oct 19, 2022 by avibootz
...