Search⌘ K

The Self Type

Explore how the Self keyword in Rust acts as a shorthand for the current type in method implementations. Understand its role in simplifying code by avoiding repeated type names, especially for return types, and get prepared for its importance in trait implementations later in the course.

We'll cover the following...

This is a minor point for now. Let’s look at our new static method again:

Rust 1.40.0
impl Fruit {
fn new() -> Fruit {
Fruit { apples: 10, bananas: 5 }
}
}

We had to mention the type Fruit three times, in the impl block itself, the return ...