Rust newbie here: I'm trying to take arguments from the commandline and then match against them. Apparently match
needs to match on &str
s rather than String
s, so I'm trying to use this answer to convert env::args()
, like so:
let args = env::args().map(AsRef::as_ref).collect::<Vec<_>>();
match &args[1..] {
["hello"] => println!("Hello, world!"),
// ...
_ => eprintln!("unknown command: {:?}", args),
}
However, the as_ref
part is failing to compile:
error[E0631]: type mismatch in function arguments
--> src/main.rs:4:32
|
4 | let args = env::args().map(AsRef::as_ref).collect::<Vec<_>>();
| --- ^^^^^^^^^^^^^
| | |
| | expected signature of `fn(String) -> _`
| | found signature of `for<'r> fn(&'r _) -> _`
| required by a bound introduced by this call
I'm having trouble understanding this error:
- When it's talking about the expected and found signatures, is it referring to the
map
parameter, or the passed-in function? - How do I understand
for<'r> fn(&'r _) -> _
? I've not seen this syntax before (especially thefor
part) so I don't know how to interpret the problem.