Created
July 27, 2020 05:24
-
-
Save ravirajawasthi/1a6188d704ae2a77690f413be236d93a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
fn main() { | |
let N: usize; | |
loop { | |
println!("How many elements would you like to enter : "); | |
let mut n = String::new(); | |
io::stdin().read_line(&mut n).expect("Input needed"); | |
match n.trim().parse::<usize>() { | |
Ok(val) => { | |
println!("N : {}", val); | |
N = n | |
.trim() | |
.parse::<usize>() | |
.expect("Most unexpected scenario, World is about to end!"); | |
break; | |
} | |
Err(err) => { | |
println!("Error {}", err); | |
println!("Try again!"); | |
} | |
} | |
} | |
let mut vector: Vec<f64> = Vec::new(); | |
let mut count = 0; | |
loop { | |
if count >= N { | |
break; | |
} else { | |
let mut input = String::new(); | |
match io::stdin().read_line(&mut input) { | |
Err(_) => { | |
println!("Give input!!"); | |
continue; | |
} | |
Ok(_) => { | |
let n = input.trim().parse::<f64>(); | |
match n { | |
Ok(val) => { | |
vector.push(val); | |
count += 1; | |
} | |
Err(_) => { | |
println!("Can't parse input: Try again"); | |
continue; | |
} | |
} | |
} | |
} | |
} | |
} | |
let sum = &vector.into_iter().sum::<f64>(); | |
let len = &vector.len(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment