I'm assuming this is a text file since you mentioned that "each integer is in different line".
You don't need to know how many lines there are. Instead, just iterate over the lines and parse out the i64
from each line. Here're a couple of versions to get you started, with the first being more "functional":
use std::fs::File;
use std::io::{BufRead, BufReader, Error, ErrorKind, Read};
fn read<R: Read>(io: R) -> Result<Vec<i64>, Error> {
let br = BufReader::new(io);
br.lines()
.map(|line| line.and_then(|v| v.parse().map_err(|e| Error::new(ErrorKind::InvalidData, e))))
.collect()
}
fn read2<R: Read>(io: R) -> Result<Vec<i64>, Error> {
let br = BufReader::new(io);
let mut v = vec![];
for line in br.lines() {
v.push(line?
.trim()
.parse()
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?);
}
Ok(v)
}
fn main() -> Result<(), Error> {
let vec = read(File::open("/some/path/to/file")?)?;
// use `vec` for whatever
Ok(())
}