Relative Paths with super in Rust

0
In Rust, we can construct relative paths that begin in the parent module, rather than the current module or the crate root, by using `super` at the start of the path. This is similar to starting a filesystem path with the `..` syntax.

`super` for Relative Paths

Using `super` allows us to reference an item that we know is in the parent module. This can make rearranging the module tree easier when the module is closely related to the parent, but the parent might be moved elsewhere in the module tree someday.

Consider the following code that models a situation in which a chef fixes an incorrect order and personally brings it out to the customer:
Rust
// Filename: src/lib.rs

fn deliver_order() {}

mod back_of_house {
    fn fix_incorrect_order() {
        cook_order();
        super::deliver_order();
    }

    fn cook_order() {}
}
In this example, the function `fix_incorrect_order` defined in the `back_of_house` module calls the function `deliver_order` defined in the parent module by specifying the path to `deliver_order` starting with `super`.

Benefits of Using `super`

The `fix_incorrect_order` function is in the `back_of_house` module, so we can use `super` to go to the parent module of `back_of_house`, which in this case is `crate`, the root. From there, we look for `deliver_order` and find it. Success!

We think the `back_of_house` module and the `deliver_order` function are likely to stay in the same relationship to each other and get moved together should we decide to reorganize the crate’s module tree. Therefore, we used `super` so we’ll have fewer places to update code in the future if this code gets moved to a different module.

Using `super` for relative paths in Rust can make your code more maintainable and flexible. It allows you to easily reference items in the parent module and can simplify the process of reorganizing your module tree.

Post a Comment

0Comments
Post a Comment (0)