January 2019
Beginner to intermediate
554 pages
13h 31m
English
There are times when you are using a type that has the same set of methods as one of its implemented traits. In those situations, Rust provides us with the uniform function call syntax that works for calling methods that are either on types themselves or come from a trait. Consider the following code:
// ufcs.rstrait Driver { fn drive(&self) { println!("Driver's driving!"); }}struct MyCar;impl MyCar { fn drive(&self) { println!("I'm driving!"); }}impl Driver for MyCar {}fn main() { let car = MyCar; car.drive();}
The preceding code has two methods with the same name, drive. One of them is an inherent method and the other comes from the trait, Driver. If we compile and run this, we get the following output: ...
Read now
Unlock full access