Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest items be borrowed in for i in items[x..] #87994

Closed
Nicholas-Baron opened this issue Aug 13, 2021 · 1 comment · Fixed by #88578
Closed

Suggest items be borrowed in for i in items[x..] #87994

Nicholas-Baron opened this issue Aug 13, 2021 · 1 comment · Fixed by #88578
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nicholas-Baron
Copy link
Contributor

Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5bb0aaa179979e54df877b3aafe9f507

use std::fmt::Display;

fn pretty_list(v: &Vec<impl Display>){
    print!("{}", v[0]);
    for item in v[1..] {
        print!(", {}", item);
    }
}

fn main(){
    let data = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    pretty_list(&data);
}

The current output is:

error[E0277]: the size for values of type `[impl Display]` cannot be known at compilation time
 --> src/main.rs:5:17
  |
5 |     for item in v[1..] {
  |                 ^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[impl Display]`
  = note: required because of the requirements on the impl of `IntoIterator` for `[impl Display]`
  = note: required by `into_iter`

error[E0277]: `[impl Display]` is not an iterator
 --> src/main.rs:5:17
  |
5 |     for item in v[1..] {
  |                 ^^^^^^ `[impl Display]` is not an iterator
  |
  = help: the trait `Iterator` is not implemented for `[impl Display]`
  = note: required because of the requirements on the impl of `IntoIterator` for `[impl Display]`
  = note: required by `into_iter`

error: aborting due to 2 previous errors

Ideally, the output should note that &v[1..] is a possible solution to these errors.

@Nicholas-Baron Nicholas-Baron added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 13, 2021
@Nicholas-Baron
Copy link
Contributor Author

@rustbot label: +D-newcomer-roadblock.

@rustbot rustbot added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Aug 13, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 9, 2021
…erence-to-for-loop-iter, r=nagisa

fix(rustc): suggest `items` be borrowed in `for i in items[x..]`

Fixes rust-lang#87994
Manishearth added a commit to Manishearth/rust that referenced this issue Sep 10, 2021
…erence-to-for-loop-iter, r=nagisa

fix(rustc): suggest `items` be borrowed in `for i in items[x..]`

Fixes rust-lang#87994
@bors bors closed this as completed in 733bdd0 Sep 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants