0
0
Rustprogramming~30 mins

Compound data types in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with Compound Data Types in Rust
๐Ÿ“– Scenario: You are building a simple program to store and display information about a book in a library system.
๐ŸŽฏ Goal: Create a compound data type using a struct to hold book details, then use it to store and print information about a specific book.
๐Ÿ“‹ What You'll Learn
Create a struct named Book with fields for title, author, and year
Create a variable of type Book with specific values
Access the fields of the Book variable
Print the book information in a readable format
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Compound data types like structs help organize related information together, such as details about books, users, or products in real applications.
๐Ÿ’ผ Career
Understanding how to create and use structs is essential for Rust programming jobs, especially in systems programming, web backend, and data modeling.
Progress0 / 4 steps
1
Define the Book struct
Write a struct named Book with three fields: title and author of type String, and year of type u32.
Rust
Need a hint?

Use struct Book { title: String, author: String, year: u32 } to define the compound data type.

2
Create a Book instance
Create a variable named my_book of type Book with title set to "The Rust Book", author set to "Steve", and year set to 2021.
Rust
Need a hint?

Use let my_book = Book { title: String::from("The Rust Book"), author: String::from("Steve"), year: 2021 };

3
Access the fields of my_book
Create three variables named book_title, book_author, and book_year that get the values from my_book.title, my_book.author, and my_book.year respectively.
Rust
Need a hint?

Use let book_title = &my_book.title; and similar for author and year.

4
Print the book information
Use println! to display the book information in this exact format: "Title: The Rust Book, Author: Steve, Year: 2021" using the variables book_title, book_author, and book_year.
Rust
Need a hint?

Use println!("Title: {}, Author: {}, Year: {}", book_title, book_author, book_year);