Your First Cargo Project

RustBeginner
Practice Now

Introduction

Rust programs are commonly organized and run with Cargo, Rust's project and build tool. In this lab, you will finish a small Cargo project in a Linux terminal. No previous terminal, editor, or Rust experience is required.

You will learn how to move into a project folder, edit a Rust source file with nano, ask Cargo to check the code, and run the finished program. The lab checks both your source code and the program's exact output.

Enter the Project and Edit Its Source

In this step, you will locate the prepared project and make your first small Rust edit with nano.

The terminal is the text area where you type commands. A command is an instruction for the computer. Click inside the terminal first so it has keyboard focus, type the following command, and press Enter:

pwd

pwd means print working directory. A directory is another name for a folder. The output shows the folder you are currently in, usually /home/labex.

Next, list the items in that folder:

ls

ls means list. You should see a folder named project.

Use cd, which means change directory, to enter the prepared Rust project. The path begins with /, so it is an absolute path: it names the location starting from the top of the filesystem rather than from your current directory.

cd /home/labex/project/hello-rust

The terminal normally prints nothing when cd succeeds. List the project contents:

ls

You should see Cargo.toml and src. Cargo reads Cargo.toml to learn the package name, version, Rust edition, and dependencies. The src directory holds Rust source code; this program starts in src/main.rs.

Open the source file in nano, a beginner-friendly terminal text editor:

nano src/main.rs

src/main.rs is a relative path. Because it does not begin with /, the terminal looks for it inside your current directory, /home/labex/project/hello-rust. This is why entering the project with cd first matters.

The file appears inside the terminal. Use the arrow keys to move the cursor. Change only the five underscore characters so that the line becomes:

    println!("Hello from LabEx!");

At the bottom of nano, the ^ symbol means the Ctrl key. Save the file with Ctrl+O (hold Ctrl and press O), then press Enter to confirm the filename. Exit with Ctrl+X.

Reopen the same file to prove that saving wrote the change to disk:

nano src/main.rs

Confirm that the line contains Hello from LabEx!. You do not need to edit it again, so exit with Ctrl+X. Reopening a saved file is a useful check whenever you are unsure whether an editor change persisted.

Display the saved file without opening the editor again:

cat src/main.rs

cat prints a file's contents in the terminal. Unlike nano, it only displays the file and does not provide an editing screen. Confirm that you see Hello from LabEx! and no underscores inside the message.

Add a Message and Check the Code

In this step, you will understand the small program, add one line, and let Cargo check it for mistakes.

Your terminal should still be in /home/labex/project/hello-rust. If you are unsure, pwd shows the current directory. Open the source file again:

nano src/main.rs

The program has a few important Rust building blocks:

  • fn main() defines the main function, where this program starts.
  • The opening and closing braces, { and }, surround the function's instructions.
  • println! displays a line of text. The ! tells you that println! is a Rust macro.
  • Text between double quotation marks is a string literal.
  • The semicolon ; ends this instruction.

Move the cursor to the end of the existing println! line, press Enter, and add this second instruction before the closing brace:

    println!("I am learning Rust with Cargo.");

The complete file should now look like this:

fn main() {
    println!("Hello from LabEx!");
    println!("I am learning Rust with Cargo.");
}

Save with Ctrl+O, press Enter, and exit with Ctrl+X.

Now ask Cargo to check whether the package can compile:

cargo check

cargo check analyzes the code and reports errors without producing the final runnable program. The first check may take a moment. A successful result ends with a line containing Finished.

If Cargo reports an error, compare your file with the complete example. Pay special attention to quotation marks, parentheses, semicolons, and braces. Use nano src/main.rs to correct the file, save it, and run cargo check again.

Run the Program with Cargo

In this step, you will build and run your checked Rust program.

Run the following command from /home/labex/project/hello-rust:

cargo run

cargo run performs two connected jobs: it compiles the package into a runnable program and then starts that program. Cargo may print a few build-status lines before your own output.

Near the end, you should see these two lines:

Hello from LabEx!
I am learning Rust with Cargo.

Run the program once more with Cargo's status messages hidden:

cargo run --quiet

The --quiet option reduces Cargo's own output, so only the two lines from your program should appear. You have now completed the basic Cargo workflow: edit, check, and run.

Summary

You navigated a Linux terminal, edited and saved Rust code with nano, recognized the main function and println! macro, checked a Cargo package, and ran it. The edit-check-run cycle you practiced is the foundation for the Rust projects that follow.