I'm attempting to parse a series of files corresponding to filenames into ELF objects using the goblin
crate:
use goblin::elf::Elf;
use std::error::Error;
use std::fs;
fn main() -> Result<(), Box<dyn Error>> {
let files = vec!["foo".to_string(), "bar".to_string()];
let elfs = files.into_iter().map(fs::read).collect::<Result<Vec<Vec<u8>>,_>>()?;
let _parsed_elfs = elfs.into_iter().map(|x| Elf::parse(x.as_slice())).collect::<Result<Vec<Elf>, _>>();
return Ok(());
}
with Cargo.toml
[package]
name = "fold"
version = "0.1.0"
authors = ["Akshat Mahajan"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
goblin = "0.4.3"
However, the borrow checker complains:
error[E0515]: cannot return value referencing function parameter `x`
--> src/main.rs:44:49
|
44 | let _parsed_elfs = elfs.into_iter().map(|x| Elf::parse(x.as_slice())).collect::<Result<Vec<Elf>, _>>()?;
| ^^^^^^^^^^^-^^^^^^^^^^^^
| | |
| | `x` is borrowed here
| returns a value referencing data owned by the current function
error: aborting due to previous error
What is the correct operation to fix this error?
Things I have tried:
- Reading How to fix cannot return value referencing function parameter error in Rust. However, I don't think the solution applies here, since I need the result of
Elf::parse
which is not an owned object.