Why can't you call another method from inside the impl?

I’m trying to build a library with the general frame something like:
pub struct Something {something}

impl Something {
fn x(){}

fn y() {
Something::x();
}
}

But this throws an error that Something has no method x.

Why is this the case? I had to make a copy of X method outside the impl just to be able to call it from inside the impl methods. Is there no way to get past this in a more elegant manner?

1 Like

This is due to compiler limitations. If you make another impl block you can use the methods defined in the previous impl blocks. Eg.

struct Something {}

impl Something {
    pub fn foo(self) {}
}

impl Something {
    pub fn bar(self) {
        self.foo()
    }
}

Hopefully this will be fixed in future compiler updates, but in the mean time you can use the above workaround.

3 Likes