Rust入门

Rust入门

安装 Rust

使用 rustup安装Rust,rustup是一个用于管理 Rust 版本,及其相关工具的命令行工具。安装过程会因操作系统而略有不同。

类似Linix的系统

  • 打开终端 terminal

  • 运行以下命令,下载并执行rustup安装脚本

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

  • 按照屏幕上的指示操作。默认的"标准安装",通常足以满足大多数用户的需求

Windows

  • 从 Rust 官方网站 (rust-lang.org/tools/install) 下载 rustup-init.exe 安装程序
  • 运行下载的 rustup-init.exe 文件
  • 按照屏幕上的说明进行操作。通常情况下,默认的"标准安装",通常足以满足大多数用户的需求
  • 在Windows上编译Rust,通常需要C++构建工具,这些工具通常由 Visual Studio 提供。如果未安装这些工具,rustup 安装程序可能会提示您安装,或者您可能需要单独安装它们

验证安装

安装完成后,通过打开一个新的终端,或命令提示符并运行以下命令来验证

复制代码
cargo --version

屏幕输出

复制代码
cargo 1.91.0 (ea2d97820 2025-10-10)

该命令输出 Cargo 的版本,cargo 是 Rust 的构建工具和包管理器,以确认 Rust 已成功安装。

可以用下列的命令,列出cargo工具的功能

复制代码
~$ cargo --list

屏幕输出

复制代码
Installed Commands:
    add                  Add dependencies to a Cargo.toml manifest file
    b                    alias: build
    bench                Execute all benchmarks of a local package
    build                Compile a local package and all of its dependencies
    c                    alias: check
    check                Check a local package and all of its dependencies for errors
    clean                Remove artifacts that cargo has generated in the past
    clippy               Checks a package to catch common mistakes and improve your Rust code.
    config               Inspect configuration values
    d                    alias: doc
    doc                  Build a package's documentation
    fetch                Fetch dependencies of a package from the network
    fix                  Automatically fix lint warnings reported by rustc
    fmt                  Formats all bin and lib files of the current crate using rustfmt.
    generate-lockfile    Generate the lockfile for a package
    git-checkout         REMOVED: This command has been removed
    help                 Displays help for a cargo subcommand
    info                 Display information about a package
    init                 Create a new cargo package in an existing directory
    install              Install a Rust binary
    locate-project       Print a JSON representation of a Cargo.toml file's location
    login                Log in to a registry.
    logout               Remove an API token from the registry locally
    metadata             Output the resolved dependencies of a package, the concrete used versions including overrides, in machine-readable format
    miri
    new                  Create a new cargo package at <path>
    owner                Manage the owners of a crate on the registry
    package              Assemble the local package into a distributable tarball
    pkgid                Print a fully qualified package specification
    publish              Upload a package to the registry
    r                    alias: run
    read-manifest        DEPRECATED: Print a JSON representation of a Cargo.toml manifest.
    remove               Remove dependencies from a Cargo.toml manifest file
    report               Generate and display various kinds of reports
    rm                   alias: remove
    run                  Run a binary or example of the local package
    rustc                Compile a package, and pass extra options to the compiler
    rustdoc              Build a package's documentation, using specified custom flags.
    search               Search packages in the registry. Default registry is crates.io
    t                    alias: test
    test                 Execute all unit and integration tests and build examples of a local package
    tree                 Display a tree visualization of a dependency graph
    uninstall            Remove a Rust binary
    update               Update dependencies as recorded in the local lock file
    vendor               Vendor all dependencies for a project locally
    verify-project       DEPRECATED: Check correctness of crate manifest.
    version              Show version information
    yank                 Remove a pushed crate from the index

cargo - Rust 构建工具和包管理器

当使用rustup安装Rust时,构建工具和包管理器cargo。

常用的cargo功能

  • cargo new,产生一个应用程序项目

  • cargo build,编译。链接应用程序项目

  • cargo run,运行应用程序

  • cargo test,测试应用程序

  • cargo doc,生成应用程序项目文档

  • cargo publish,将库发布到 crates.io

    source HOME/.cargo/env
    cargo new hello cd hello
    cargo build cargo run

屏幕输出

复制代码
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/hello`
Hello, world!
相关推荐
墩墩大魔王丶20 分钟前
macOS Rust 安装教程:自定义 CARGO_HOME 和 RUSTUP_HOME
后端
重生之后端学习27 分钟前
Java入门
java·开发语言·职场和发展
碧海蓝天202232 分钟前
C++法则24:在标准 C++ 中,没有任何可移植的方式判断指针 T* pt 指向的内存位置是否已经 构造了对象,程序员必须手动跟踪哪些元素已构造。
java·开发语言·c++
代码不加糖40 分钟前
Proxy能够监听到对象中的对象的引用吗?
开发语言·前端·javascript
charlie1145141911 小时前
现代C++指南:Lambda,让我们用另一种方式持有函数
开发语言·c++
qq3621967051 小时前
阿里裁员新消息(2026最新动态汇总)
java·开发语言·前端
进阶的小名1 小时前
Spring Boot SSE + Nginx 配置:解决 EventSource 不实时返回、连接超时、流式响应被缓冲问题
spring boot·后端·nginx
.千余2 小时前
【C++】模板进阶全解:非类型参数|全特化|偏特化|分离编译完全指南
开发语言·c++·笔记·学习·其他
代码改善世界2 小时前
【C++进阶】C++11:列表初始化、右值引用与移动语义、完美转发全解析
java·开发语言·c++
scx_link2 小时前
通过git bash在本地创建分支,并推送到远程仓库中
开发语言·git·bash