Skip to content

Unhelpful suggestions trying to use array::set #96834

Closed
@jdonszelmann

Description

@jdonszelmann

Given the following code:
playground

fn main() {
    let mut a = [0u8; 1];
    a.set(0, 3);
}

The current output is:

/tmp/tmp.pvMJNmiDgb/bad_error/src (git)-[main] % cargo run
   Compiling bad_error v0.1.0 (/tmp/tmp.pvMJNmiDgb/bad_error)
error[E0599]: no method named `set` found for array `[u8; 1]` in the current scope
   --> src/main.rs:3:7
    |
3   |     a.set(0, 3);
    |       ^^^
    |
   ::: /home/jonathan/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/pin.rs:659:12
    |
659 |     pub fn set(&mut self, value: P::Target)
    |            --- the method is available for `Pin<&mut [u8; 1]>` here
    |
help: consider wrapping the receiver expression with the appropriate type
    |
3   |     Pin::new(&mut a).set(0, 3);
    |     +++++++++++++  +
help: there is an associated function with a similar name
    |
3   |     a.get(0, 3);
    |       ~~~

For more information about this error, try `rustc --explain E0599`.
error: could not compile `bad_error` due to previous error

image

I don't think suggesting to use Pin here helps users. I tried to use set, since there's also a get so it felt natural to me that set should also exist. It may be a different issue altogether if having no set on arrays is expected, but the error message really bothered me. I tried to do a, what felt to me, trivial operation on an array, and the first suggestion is to Pin it, something which I know is useless. However, unexperienced rust users may not know this. It also doesn't solve the issue. There would be a set method on pin, but it wouldn't do what you expect it to do.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions