How can I return the result of a File::open
operation in Rust?
The compiler complains that the result is owned by the function, but provides no suggestions, and there's no useful suggestions easily searchable on the web.
Code:
use std::io::{Read, Seek};
trait ReadSeek: Read + Seek {}
fn get_file_reader(path: &'static str) ->
Result<&mut dyn ReadSeek, Box<dyn Error>> {
Ok(&mut File::open(path)?)
}
Error:
error[E0515]: cannot return value referencing temporary value
--> src/archive/zipfile.rs:32:9
|
32 | Ok(&mut File::open(path)?)
| ^^^^^^^^-----------------^
| | |
| | temporary value created here
| returns a value referencing data owned by the current function