RUSTPRINT

Search RustPrint by question, error, syntax, or task.

← Start with Rust

START 05 // FROM FILE TO PROJECT

Your first Rust project

A real Rust project is more than one .rs file. Cargo gives the project a predictable structure so the compiler, dependencies, tests, and build output all have a place to live.

The important mental shift is simple:you usually do not compile Rust projects by manually calling rustc. You let Cargo organize the project and drive the compiler for you.

Create the project

In a terminal, run:

CREATE A NEW RUST APPLICATION
cargo new hello-rust
cd hello-rust

Cargo creates a new directory named hello-rust and puts a minimal Rust application inside it. By default, cargo new creates a binary project: something that can be run as a program.

What did Cargo create?

PROJECT SHAPE
hello-rust/
├── Cargo.toml
└── src/
    └── main.rs
src/main.rs

The main Rust source file for this small executable program.

Cargo.toml

The project manifest: name, version, Rust edition, and dependencies live here.

Look at the code before running it

The generated program is the classic “Hello, world!” example. RustPrint's equivalent checked example is:

EXAMPLE: CI-CHECKEDCOMPILED · examples/getting-started/src/main.rs#start-first-project-hello
println!("Hello, Rust!");

The function named main is where this executable begins. The println! call writes a line of text to standard output. That is enough context for now.

Run it

FROM THE PROJECT DIRECTORY
cargo run

Cargo checks what needs to be built, invokes the Rust compiler, and then runs the resulting program. On the first run you will see build status text followed by the program's own output.

WHY NOT JUST CLICK “RUN” IN AN EDITOR?

You absolutely can use an editor or IDE button later. Learning cargo run once is still useful because it shows what the editor is doing for you and works everywhere a Rust toolchain is installed.

Three Cargo commands worth recognizing immediately

cargo run

Build the current binary if needed, then run it.

cargo check

Check whether the project compiles without producing the final executable, which is often faster.

cargo build

Compile the project and keep the executable in Cargo's target directory.

cargo test

Build and run the project's tests when it has some.

What are target/ and Cargo.lock?

After building, Cargo creates a target directory for build output. It can also createCargo.lock, which records exact dependency versions selected for the project. Cargo manages that lock file for you.

None of this is special ceremony you need to master before writing code. It is simply the standard project shape that lets Rust tools agree on where everything is.

PRIMARY SOURCES

Official walkthroughs behind these commands