我正尝试编写cat
命令来学习Rust,但似乎无法将命令行参数转换为读取结构。
use std::{env, io};
use std::fs::File;
fn main() {
for arg in env::args().skip(1) {
let reader = match arg.as_str() {
"-" => io::stdin(),
path => File::open(&path).unwrap(),
};
}
}
错误:
error[E0308]: match arms have incompatible types
--> src/main.rs:6:22
|
6 | let reader = match arg.as_str() {
| ^ expected struct `std::io::Stdin`, found struct `std::fs::File`
|
= note: expected type `std::io::Stdin`
= note: found type `std::fs::File`
note: match arm with an incompatible type
--> src/main.rs:8:21
|
8 | path => File::open(&path).unwrap(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
似乎无法使用多态方式匹配特质实现者 (相关)。我该如何将File
或Stdin
用作阅读器?
open_reader
中,那么这个方法就无法奏效了。但确实是个好主意。 - Lukas Kalbertodt