我正在阅读文档,尝试编写一些基本的文件I/O代码来帮助我学习Rust。
以下代码无法编译:
以下代码无法编译:
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())
}
出现编译错误:
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`
我应该如何编写此函数,以返回传入的Path
中包含的Path
集合?
core::marker::Sized
未为类型[u8]
实现”。* - Shepmaster