An important thing to note is that the error said associated item. enum Foo { Baz }
doesn't have associated items. A trait can have an associated item:
trait FooBaz { type Baz }
// ^~~~~~~~ - associated item
To summarize:
Why can't Self
be used in this situation?
Because of this issue. RFC 2338 has not been implemented yet.
Self
seems to act as a type alias, albeit with some modifications.
Where exactly can Self
be used?
Self can only be used in traits and impl
s. This code:
struct X {
f: i32,
x: &Self,
}
Outputs the following:
error[E0411]: cannot find type `Self` in this scope
--> src/main.rs:3:9
|
3 | x: &Self,
| ^^^^ `Self` is only available in traits and impls
This is possibly a temporary situation and might change in the future!
More precisely, Self
should be used only as part of method signature (e.g. fn self_in_self_out(&self) -> Self
) or to access an associated type:
enum Foo {
Baz,
}
trait FooBaz {
type Baz;
fn b(&self) -> Self::Baz; // Valid use of `Self` as method argument and method output
}
impl FooBaz for Foo {
type Baz = Foo;
fn b(&self) -> Self::Baz {
let x = Foo::Baz as Self::Baz; // You can use associated type, but it's just a type
x
}
}
I think user4815162342 covered the rest of the answer best.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…