Match with vector index inside a map results in compiler error
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 7k
- Forks
- 1.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 24
Description
I'm using rustfmt 1.8.0 and rustc 1.86.0. Here is a reproducible example:
Before formatting
(compiles with no errors or warnings)
fn main() {
let data = vec!["hello", "world", "test"];
let result: Vec<String> = data
.iter()
.map(|x|
match simulate_operation(x) {
Ok(v) => v,
Err(e) => vec![format!("Error: {}", e)],
}
[0].clone()
)
.collect();
println!("{:?}", result);
}
fn simulate_operation(input: &str) -> Result<Vec<String>, &'static str> {
if input.len() > 4 {
Ok(vec![format!("SUCCESS: {}", input)])
} else {
Err("too short")
}
}
Pay attention to the match simulate_operation(x) line in main().
After formatting:
fn main() {
let data = vec!["hello", "world", "test"];
let result: Vec<String> = data
.iter()
.map(|x| {
match simulate_operation(x) {
Ok(v) => v,
Err(e) => vec![format!("Error: {}", e)],
}[0]
.clone()
})
.collect();
println!("{:?}", result);
}
fn simulate_operation(input: &str) -> Result<Vec<String>, &'static str> {
if input.len() > 4 {
Ok(vec![format!("SUCCESS: {}", input)])
} else {
Err("too short")
}
}
rustfmt wraps the match statement with a {} pair. This results in the following error:
error[E0277]: a value of type `Vec<String>` cannot be built from an iterator over elements of type `[{integer}; 1]`
--> rustfmterr.rs:13:10
|
13 | .collect();
| ^^^^^^^ value of type `Vec<String>` cannot be built from `std::iter::Iterator<Item=[{integer}; 1]>`
|
= help: the trait `FromIterator<[{integer}; 1]>` is not implemented for `Vec<String>`
but trait `FromIterator<String>` is implemented for it
= help: for that trait implementation, expected `String`, found `[{integer}; 1]`
note: the method call chain might not have had the expected associated types
--> rustfmterr.rs:6:10
|
2 | let data = vec!["hello", "world", "test"];
| ------------------------------ this expression has type `Vec<&str>`
...
5 | .iter()
| ------ `Iterator::Item` is `&&str` here
6 | .map(|x| {
| __________^
7 | | match simulate_operation(x) {
8 | | Ok(v) => v,
9 | | Err(e) => vec![format!("Error: {}", e)],
10 | | }[0]
11 | | .clone()
12 | | })
| |__________^ `Iterator::Item` changed to `[{integer}; 1]` here
note: required by a bound in `collect`
--> /build/rustc-1.86.0-src/library/core/src/iter/traits/iterator.rs:1967:5
error: aborting due to 2 previous errors
This is because the match statement gets separated from the vector index. Instead of a {} pair, the match statement should be surrounded in a () pair (my current workaround before formatting), or should not be surrounded in anything.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproducible example in rustfmterr.rs, focusing on the match expression in main followed by [0]. Check how rustfmt formats this construct and verify that formatting preserves the intended expression grouping, either with parentheses or without added braces, so the formatted example still compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100