Closed
Description
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)