Skip to content

Help message: use . instead of :: when accessing a method of an object #101749

Closed
@m-spitfire

Description

@m-spitfire

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

struct Rectangle {
    width: i32,
    height: i32,
}
impl Rectangle {
    fn new(width: i32, height: i32) -> Self {
        Self { width, height }
    }
    fn area(&self) -> i32 {
        self.height * self.width
    }
}

fn main() {
    let width = 3;
    let height = 4;
    let rect1 = Rectangle::new(width, height);
    println!("{}", rect1::area());
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0433]](https://doc.rust-lang.org/stable/error-index.html#E0433): failed to resolve: use of undeclared crate or module `rect1`
  --> src/main.rs:18:20
   |
18 |     println!("{}", rect1::area());
   |                    ^^^^^ use of undeclared crate or module `rect1`

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

Ideally the output should look like(additionally):

help: use .area() instead of ::area() when accessing method of an object

The output is the same in nightly too.

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