In Rust you don't have to define all struct fields explicitly, for example:
struct MyBox<T>(T);
impl<T> MyBox<T> {
fn new(x: T) -> MyBox<T> {
MyBox(x)
}
}
MyBox contains an instance of generic type T, but how can I refer to it? We haven't declared an explicit property for it!
The trick is to pay attention to that (T) in the struct definition, which essentially means we're operating with a tuple.
This kind of definitions is known in Rust as Tuple Structs.
In such cases, Rust allows 0-based indexing to access fields within a struct, similarly to tuples, therefore the T instance will be available by accessing self.0.
A more traditional way to declare this struct with an explicit name would be:
struct MyBox<T> {
x: T,
}
impl<T> MyBox<T> {
fn new(x: T) -> MyBox<T> {
MyBox{ x }
}
}