For Reference: I am a Java programmer who is brand new to Rust
I am trying to read a file that has 2 inputs being size of array, and elements of array.
Input:
3
1 2 3
I want to make a function that reads in a file through arguments in command line so I wrote the following code: (Note: it is super ugly as I was changing up the function)
fn getArraySize() -> usize
{
let args: Vec<String> = env::args().collect();
let input = File::open(&args[1])?;
let buffered = BufReader::new(input);
let mut counter: i32 = 0;
let arraySize: usize = 0;
for line in buffered.lines() {
if counter == 0
{
let line = String::from(line?);
arraySize = line.parse().unwrap();
println!("{}", arraySize);
counter += 1;
}
}
arraySize
}
Which is a function that sets a const value
const ARRAYSIZE: usize = getArraySize();
My problem is that I just cannot seem to figure out how to get this line to work
let input = File::open(&args[1])?;
I tried attaching .expected to it, but nothing happened. I tried everything I could find on Google, but without global variables I can change, I don't know how to get the file such that my function can read it and return arraySize.