1. 33
    Rustlings modules2: Defining public interfaces for nested modules
    57s

Rustlings modules2: Defining public interfaces for nested modules

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

README for this exercise.

Chris Biscardi: [0:00] In modules2, we have a delicious_snacks module that uses self: :fruits: :PEAR as fruit; and self: :veggies: :CUCUMBER as veggie;. You can see that inside of this module we have an additional module called fruits and additional module called veggies, which is what these use statements refer to.

[0:14] Inside of our fruit and veggie modules, we have pub const PEAR, APPLE, CUCUMBER, and CARROT. These constants are string slices with static lifetimes. In our main function we use the println! Macro to print our favorite snacks which are delicious_snacks: :fruit and delicious_snacks: :veggie.

[0:30] The Rust compiler tells us that the constant import 'fruit' is private inside of the delicious snacks module. One way we can solve this is by making our use statements public inside of the delicious_snacks module. This exposes each of the individual constants, but not the other ones involved in the fruits and veggies modules.

[0:50] Now, in the end, we have our delicious_snacks module that exports a different interface than the internal fruits and veggies modules.