1.6 Running Rust
Explanation
For reproducible checks, prefer a tiny Cargo project. Cargo creates a standard project layout, records package metadata, and gives the same basic commands to students, AI agents, and reviewers.
Use cargo run to build and execute a program. Use cargo test to build the project and run its tests.
Single .rs files can be useful for tiny experiments, especially while thinking through syntax or a small calculation. Exercises that include tests should normally use Cargo so that the test command and project layout are clear.
Things to look up
cargo newcargo runcargo testsrc/main.rssrc/lib.rstestsdirectory
Exercise
Create work/chapter_1.6/hello_rust with cargo new --lib. Add one simple function and one simple test. Run the test with cargo test.
Notes for the exercise
- Keep generated files small.
- Know which command runs the program and which command runs the tests.
- Know that
src/lib.rsis for reusable library code, whilesrc/main.rsis for a runnable program entry point. - Keep
Cargo.lockwith Cargo exercise projects so the resolved environment is visible.