Control Visibility Within Different Files Using 'pub'

This lesson teaches you how to access the module in a different file but within the same directory.

When modules get large and become cumbersome to store in a single file, it is possible to move their definitions to a separate file to make the code easier to navigate. It is possible to access a module even if it belongs to a different file. To use the module in a different file, write mod followed by the name of the file in which the module is declared.

Implicit Declaration

A block of code put in a file without wrapping in a mod block implicitly declares a module.

  • Import the module
    mod file_name
  • Call the module
    file_name::x

Where x can be any construct within the module, i.e., function, array, trait, struct.

📝 Rust code is always put in files with .rs extension

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy