Skip to main content
In this lesson, we explore a crucial feature in Rust: fully-qualified syntax. This concept becomes especially important when functions or methods share the same name but perform different actions based on their context. Fully-qualified syntax provides a clear way to specify which function implementation to invoke.

Understanding Methods in Rust

Methods in Rust are functions defined within the context of a struct, enum, or trait. They are associated with a particular instance of a type and typically take self (or a reference to it) as their first parameter, allowing the method to access or modify instance data.
The image explains "Methods" and "Self Parameter" in programming, highlighting that methods are functions tied to structures and the self parameter is used to access and modify instance data.
Because Rust allows multiple traits—or even a trait and a struct—to define methods with identical names, ambiguity may arise. For instance, if two traits and a struct each implement a method called move_forward, it becomes necessary to clarify which version of move_forward should be executed.
The image illustrates a diagram showing the concept of calling methods with the same name, specifically "move_forward," from different sources: Driver, Flyer, and struct Robot. It poses a question about which method is called when using "robot.move_forward()".
Fully-qualified syntax is essential in Rust to ensure that the correct method is called, preventing ambiguity when multiple implementations exist.

A Practical Example with Methods

Consider a scenario where we have two traits, Driver and Flyer, each defining a move_forward method. Additionally, the Robot struct provides its own implementation of move_forward. Here is how the implementations are structured:
When calling move_forward directly on a Robot instance like this:
Rust defaults to the implementation provided directly on the Robot struct, and thus prints:
To invoke the move_forward methods from the Driver or Flyer traits, you must use fully-qualified syntax. Here’s how:
The expected output of the above code is:
By clearly specifying the trait name using the double colon (::), fully-qualified syntax ensures that Rust calls the intended version of the method.

Fully-qualified Syntax with Associated Functions

Fully-qualified syntax is equally important for associated functions—those functions that do not require a self parameter. Consider the following scenario where we want to assign different names to a Robot based on its capabilities. We define a trait named GroundRobot with an associated function robot_name, and we also implement a function with the same name directly on the Robot struct.
When calling the associated function directly on the Robot struct like this:
Rust invokes the implementation defined on the Robot struct, producing the following output:
To specifically call the robot_name function from the GroundRobot trait, you must use fully-qualified syntax as shown below:
This call will output:
The general pattern for using fully-qualified syntax is:
For methods that take self, you include the instance as an argument. For associated functions, simply supply the necessary parameters after the function name.
Be cautious when using the same function name across multiple traits and structs. Without fully-qualified syntax, Rust may not know which function version to execute, leading to bugs or unexpected behavior.
Fully-qualified syntax is vital in Rust to disambiguate between different implementations and ensure that the correct function is called, thereby enhancing code clarity and maintainability.

Watch Video