0
0
Rustprogramming~15 mins

Defining modules in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Defining modules
📖 Scenario: You are organizing a small Rust program that manages book information. To keep your code clean and easy to understand, you want to split it into modules.
🎯 Goal: You will create a module named library that contains a function to display a book's title. Then you will call this function from the main program.
📋 What You'll Learn
Create a module named library
Inside library, define a public function print_book_title that prints the book title
Call library::print_book_title from the main function
💡 Why This Matters
🌍 Real World
Modules help organize large programs by grouping related code together, making it easier to maintain and understand.
💼 Career
Understanding modules is essential for writing clean, scalable Rust code in professional software development.
Progress0 / 4 steps
1
Create the library module
Create a module named library using mod library { } syntax.
Rust
Hint

Use mod library { } to define the module.

2
Add a public function print_book_title inside library
Inside the library module, define a public function named print_book_title that prints "Book title: The Rust Book".
Rust
Hint

Use pub fn print_book_title() { println!("Book title: The Rust Book"); } inside the module.

3
Call print_book_title from main
In the main function, call the function print_book_title from the library module using library::print_book_title().
Rust
Hint

Call the function with library::print_book_title(); inside main.

4
Print the output
Run the program and print the output from calling library::print_book_title().
Rust
Hint

The program should print exactly Book title: The Rust Book.