Closed
Description
When trying to pattern match against a Vec
inside a Result
or Option
, an incorrect suggestion is given, that says to append [..]
to the variable to slice it, which won't work since the variable is of type Result
/ Option
rather than being a Vec
itself.
Instead one option is to use .as_deref()
on the Result
:
https://doc.rust-lang.org/std/result/enum.Result.html#method.as_deref
Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1e834c61673228c36f08e8c294efc68d
fn main() {
let s = "1:2";
let result: Result<Vec<u64>, _> = s.split(':').map(str::parse).collect();
match result {
Ok([first, second]) => {
println!("first: {}, second: {}", first, second);
}
_ => {}
}
}
The current output is:
error[E0529]: expected an array or slice, found `Vec<u64>`
--> src/main.rs:6:12
|
5 | match result {
| ------ help: consider slicing here: `result[..]`
6 | Ok([first, second]) => {
| ^^^^^^^^^^^^^^^ pattern cannot match with input type `Vec<u64>`
Ideally the output should look like:
error[E0529]: expected an array or slice, found `Vec<u64>`
--> src/main.rs:6:12
|
5 | match result {
| ------ help: consider converting the contents of the Result to a slice using: `result.as_deref()`
6 | Ok([first, second]) => {
| ^^^^^^^^^^^^^^^ pattern cannot match with input type `Vec<u64>`