Beginner’s Guide: Creating Your First Rust Project
文章介绍如何通过Cargo安装Rust并创建第一个项目,包括安装Rust环境、使用cargo new创建新项目、编写代码、运行程序、添加依赖和测试代码等步骤,并展示了从开发到发布的完整流程。 2025-9-21 00:0:0 Author: www.hahwul.com(查看原文) 阅读量:10 收藏

A step-by-step tutorial to install Rust and create, run, test, and build your first project using Cargo.

Getting started with Rust is quick if you follow a few simple steps. In this guide, you’ll install Rust, scaffold a new project, write a tiny program, run tests, and produce a release build.

I’ll keep it minimal and practical so you can get productive fast.

1) Install Rust

  • Official installer (works on all platforms):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • Verify
rustc --version
cargo --version

2) Create a new project

Rust uses cargo for building, testing, and dependency management.

  • Binary app (recommended for CLI apps and services):
cargo new hello_rust
cd hello_rust

This creates a basic structure:

hello_rust/
├─ Cargo.toml
└─ src/
   └─ main.rs

3) Write your first program

Open src/main.rs and print a greeting:

fn main() {
    println!("Hello, Rust!");
}

Run it:

cargo run

4) Manage dependencies (when you need them)

If you add dependencies to Cargo.toml, build to install them:

cargo build

You generally won’t need this for the very first “hello world,” but it’s essential once you pull in crates like HTTP clients or web frameworks.

5) Format your code

Rust ships a formatter:

cargo fmt

Run it from the project root to format all sources.

6) Add a simple test

For a beginner-friendly pattern, wrap your code in a module and test it:

// src/main.rs
mod hello_rust {
    pub fn greet(name: &str) -> String {
        format!("Hello, {}!", name)
    }
}

fn main() {
    println!("{}", hello_rust::greet("Rust"));
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_greet_default() {
        assert_eq!(hello_rust::greet("Rust"), "Hello, Rust!");
    }

    #[test]
    fn test_greet_custom() {
        assert_eq!(hello_rust::greet("World"), "Hello, World!");
    }
}

Run tests:

cargo test
# or verbose:
cargo test -- --nocapture

7) Build a binary

Two common ways to build:

  • Debug build:
cargo build
  • Release build:
cargo build --release

Run your binary:

./target/debug/hello_rust  # debug
./target/release/hello_rust  # release
cargo tree

9) Optional: Lint with Clippy

Clippy is Rust’s official linter.

  • Lint:
cargo clippy
  • Auto-fix:
cargo clippy --fix

Visual overview

Here’s a simple flow of your first project:

 flowchart LR
  A[Install Rust] --> B[cargo new hello_rust]
  B --> C[Edit src/main.rs]
  C --> D[cargo run]
  D --> E[cargo test]
  E --> F[cargo fmt]
  F --> G[cargo build --release]
  G --> H[Run ./target/release/hello_rust]

What’s next?

  • Explore popular crates:
    • Web frameworks: Rocket, Actix-Web
    • CLI: clap
    • HTTP client: reqwest
  • Build a CLI tool, a web service, or a small library and publish it on crates.io.

You’ve just created, tested, and built your first Rust project. Keep it small, iterate quickly, and enjoy the safety and performance of Rust.


文章来源: https://www.hahwul.com/dev/rust/noob-beginners-guide-in-rust/
如有侵权请联系:admin#unsafe.sh