0

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:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
  • It looks like your question might be answered by the answers of [What is the difference between iter and into_iter?](https://stackoverflow.com/q/34733811/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Dec 21 '21 at 19:47
  • `elfs.into_iter()` -> `elfs.iter()` – Shepmaster Dec 21 '21 at 19:47
  • Okay, that worked. Please feel free to mark as duplicate! – Akshat Mahajan Dec 21 '21 at 19:49

0 Answers0