📖 Scenario: You are creating a simple Rust project that needs to use an external library to format dates. You will learn how to add and manage dependencies using Cargo, Rust's package manager.
🎯 Goal: Learn how to add a dependency to Cargo.toml, configure its version, use it in your Rust code, and print formatted dates.
📋 What You'll Learn
Create a new Rust project with Cargo
Add the chrono crate as a dependency in Cargo.toml
Use the chrono crate in main.rs to get the current date and time
Print the current date and time in a readable format
💡 Why This Matters
🌍 Real World
Managing dependencies is essential for building Rust applications that use external libraries for tasks like date handling, networking, or data processing.
💼 Career
Understanding Cargo dependency management is a key skill for Rust developers working on real-world projects, ensuring code reuse and easy updates.
Progress0 / 4 steps
1
Create a new Rust project
Use the command cargo new date_formatter in your terminal to create a new Rust project called date_formatter. Then, open the src/main.rs file and write fn main() {} to create an empty main function.
Rust
Hint
Use cargo new date_formatter to create the project, then open src/main.rs and write the main function.
2
Add the chrono dependency
Open the Cargo.toml file and add the line chrono = "0.4" under the [dependencies] section to add the chrono crate version 0.4 as a dependency.
Rust
Hint
Find the [dependencies] section in Cargo.toml and add chrono = "0.4" below it.
3
Use chrono to get current date and time
In src/main.rs, add use chrono::Local; at the top. Inside main(), write let now = Local::now(); to get the current date and time.
Rust
Hint
Import Local from chrono and call Local::now() inside main().
4
Print the formatted current date and time
Add println!("Current date and time: {}", now.format("%Y-%m-%d %H:%M:%S")); inside main() to display the current date and time in the format YYYY-MM-DD HH:MM:SS.
Rust
Hint
Use println! with now.format("%Y-%m-%d %H:%M:%S") to print the date and time.
Practice
(1/5)
1. What is the main purpose of the Cargo.toml file in a Rust project?
easy
A. To store the project's binary executable
B. To write the main Rust code logic
C. To compile the Rust project manually
D. To list and manage the project's dependencies
Solution
Step 1: Understand Cargo.toml role
The Cargo.toml file is used to declare dependencies and project metadata.
Step 2: Differentiate from other files
The Rust code logic is in src/main.rs, not Cargo.toml. Compilation is done by Cargo commands, not by the file itself.
Final Answer:
To list and manage the project's dependencies -> Option D
Quick Check:
Cargo.toml manages dependencies [OK]
Hint: Remember: Cargo.toml lists dependencies, not code [OK]
Common Mistakes:
Confusing Cargo.toml with source code files
Thinking Cargo.toml compiles code
Assuming Cargo.toml stores executables
2. Which of the following is the correct syntax to add the dependency serde version 1.0 in Cargo.toml?
easy
A. serde = "1.0"
B. serde: "1.0"
C. "serde" = 1.0
D. serde => "1.0"
Solution
Step 1: Recall Cargo.toml dependency syntax
Dependencies are listed as name = "version" pairs in Cargo.toml.
Step 2: Match correct syntax
serde = "1.0" matches the correct syntax: serde = "1.0". Other options use invalid symbols.
Final Answer:
serde = "1.0" -> Option A
Quick Check:
Dependency syntax = name = "version" [OK]
Hint: Use equals and quotes for version in Cargo.toml [OK]
Common Mistakes:
Using colon or arrow instead of equals
Omitting quotes around version
Putting quotes around dependency name incorrectly
3. Given this Cargo.toml snippet:
[dependencies]
regex = "1.5"
What happens when you run cargo build for the first time?
medium
A. Cargo throws an error because regex is not installed manually
B. Cargo ignores the regex crate and builds only your code
C. Cargo downloads and compiles the regex crate version 1.5
D. Cargo compiles your code but skips dependencies
Solution
Step 1: Understand Cargo build behavior
When building, Cargo checks dependencies listed in Cargo.toml and downloads missing ones.
Step 2: Apply to regex dependency
Since regex 1.5 is listed, Cargo downloads and compiles it automatically on first build.
Final Answer:
Cargo downloads and compiles the regex crate version 1.5 -> Option C
Blaming Rust version without checking dependency name
5. You want to use two different versions of the same crate log in your project: version 0.4 for your code and version 0.3 for a dependency. How can Cargo handle this situation?
hard
A. Cargo will fail to build due to version conflict
B. Cargo allows multiple versions of the same crate to coexist in the dependency tree
C. You must manually merge the versions into one in Cargo.toml
D. Cargo ignores the older version and uses only the newest
Solution
Step 1: Understand Cargo's version handling
Cargo supports multiple versions of the same crate in the dependency tree to avoid conflicts.
Step 2: Apply to example
Your code can use log 0.4 while a dependency uses log 0.3; Cargo compiles both separately.
Final Answer:
Cargo allows multiple versions of the same crate to coexist in the dependency tree -> Option B