Skip to content

location / scoping of Impl #120753

Closed
Closed
@stroucki

Description

@stroucki

Hi,

I am learning Rust via rust-book.cs.brown.edu and I think one of the examples had an Impl for a struct placed within a function. So I was thinking about where Impls can be placed, and whether they can be out of scope. I wrote up the below code (without the added scoping) and it worked. However, it still worked when I added a layer of scoping as below.

I consulted a friend who said this may warrant a warning from the compiler, and I should create an issue for it.

struct Foo {
  x: i32,
}

pub fn main() {
{ // will still work with or without this extra scope
  impl Foo {
    fn inc(&mut self) {
      self.x+=1;
    }
  }
} // extra scope

  let mut test = Foo{ x:1 };
  test.inc();
  println!("Foo: {}", test.x);
}

Rustc version: rustc 1.75.0 (82e1608 2023-12-21) (built from a source tarball)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-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