8

I'm reading the documentation and trying to write some basic file I/O code as a vehicle to help me learn Rust.

The following doesn't compile:

use std::fs;
use std::io;
use std::path::Path;

pub fn read_filenames_from_dir<P>(path: P) -> Result<Vec<Path>, io::Error>
where
    P: AsRef<Path>,
{
    let paths = try!(fs::read_dir(path));
    Ok(paths.unwrap())
}

With the compilation error:

error[E0277]: the trait bound `[u8]: std::marker::Sized` is not satisfied in `std::path::Path`
  --> src/main.rs:5:1
   |
5  | / pub fn read_filenames_from_dir<P>(path: P) -> Result<Vec<Path>, io::Error>
6  | | where
7  | |     P: AsRef<Path>,
8  | | {
9  | |     let paths = try!(fs::read_dir(path));
10 | |     Ok(paths.unwrap())
11 | | }
   | |_^ `[u8]` does not have a constant size known at compile-time
   |
   = help: within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
   = note: required because it appears within the type `std::path::Path`
   = note: required by `std::vec::Vec`

How should I write this function to return the collection of Paths inside the Path that's passed in?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Gavin Hope
  • 2,173
  • 24
  • 38

1 Answers1

11

You don't. Path is a type that has no size, and is only usable through indirection (such as &Path or Box<Path>). In this sense, it is like the type str or [u8] — neither can be directly used, only indirectly.

What you probably want is a PathBuf, which represents an owned path. It is the equivalent of String for &str and Vec<u8> for &[u8].

After changing the return type, you have to properly map the results of the iterator to create your desired type:

use std::{
    fs, io,
    path::{Path, PathBuf},
};

pub fn read_filenames_from_dir<P>(path: P) -> Result<Vec<PathBuf>, io::Error>
where
    P: AsRef<Path>,
{
    fs::read_dir(path)?
        .into_iter()
        .map(|x| x.map(|entry| entry.path()))
        .collect()
}

fn main() {
    println!("{:?}", read_filenames_from_dir("/etc"));
}

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    What *is* a Path anyway? Doing a source code dive, I get that `Path` contains `OsStr` which contains something called `sys::os_str::Slice` which I can't find an actual definition for. I assume it's basically a few layers of abstraction over a `[u8]`? – Linear May 25 '16 at 20:17
  • 2
    @Jsor *basically a few layers of abstraction over a [u8]* — yes, [on Unix-like platforms](https://github.com/rust-lang/rust/blob/1.8.0/src/libstd/sys/unix/os_str.rs#L27-L29). Windows [is different](https://github.com/rust-lang/rust/blob/1.8.0/src/libstd/sys/windows/os_str.rs#L46-L48). This bit of abstraction leaks out in the OPs error message: "`core::marker::Sized` is not implemented for the type `[u8]`". – Shepmaster May 25 '16 at 20:21