为什么这段代码不能编译?
use std::{fs, path::Path};
fn main() {
let dir = Path::new("../FileSystem");
if !dir.is_dir() {
println!("Is not a directory");
return;
}
for item in try!(fs::read_dir(dir)) {
let file = match item {
Err(e) => {
println!("Error: {}", e);
return;
}
Ok(f) => f,
};
println!("");
}
println!("Done");
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误
error[E0308]: mismatched types
--> src/main.rs:11:17
|
11 | for item in try!(fs::read_dir(dir)) {
| ^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `std::result::Result`
|
= note: expected type `()`
found type `std::result::Result<_, _>`
= note: …Run Code Online (Sandbox Code Playgroud) 我想实现这个Shl特性Vec,代码如下.这会使事情变得vec << 4可能,这对于它来说是个不错的选择vec.push(4).
use std::ops::Shl;
impl<T> Shl<T> for Vec<T> {
type Output = Vec<T>;
fn shl(&self, elem: &T) -> Vec<T> {
self.push(*elem);
*self
}
}
fn main() {
let v = vec![1, 2, 3];
v << 4;
}
Run Code Online (Sandbox Code Playgroud)
编译失败,出现以下错误:
无法提供扩展实现,其中特征和类型都未在此包中定义[E0117]
要么
type参数
T必须用作某些本地类型的类型参数(例如MyStruct<T>); 只有当前包中定义的特征才能为类型参数实现[E0210]
据我了解,我必须修补stdlib,更具体地来说是collections::vec板条箱.有没有其他方法可以更改此代码以成功编译?