Question

Here's what I'm trying to do: open all the command line arguments as (binary) files and read bytes from them. The constantly changing syntax here is not conductive to googling, but here's what I've figured out so far:

use std::io::{File, result};
use std::path::Path;
use std::os;

fn main() {
    let args = os::args();
    let mut iter = args.iter().skip(1); // skip the program name

    for file_name in iter {
        println(*file_name);
        let path = &Path::new(*file_name);
        let file = File::open(path);
    }
}

Here's the issue:

test.rs:44:31: 44:41 error: cannot move out of dereference of & pointer
test.rs:44         let path = &Path::new(*file_name);

I've hit a brick wall here because while I'm fine with pointers in C, my understanding of the different pointer types in rust is practically non-existent. What can I do here?

Was it helpful?

Solution

Try &Path::new(file_name.as_slice())

Unfortunately, due to the trait argument that Path::new() takes, if you pass it a ~str or ~[u8] it will try and consume that type directly. And that's what you're passing with *file_name. Except you can't move out of a pointer dereference in Rust, which is why you're getting the error.

By using file_name.as_slice() instead (which is equivalent, in this case, to (*file_name).as_slice(), but Rust will do the dereference for you) it will convert the ~str to a &str, which can then be passed to Path::new() without a problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top