rust圣经笔记 1.cargo

307 阅读2分钟

安装

rustup是rust提供给开发者福音,让开发者可以很简单的进入Rust的世界

Rust依赖libc和链接器linker 所以遇到报错需要安装一个C语言的编译器如GCC之类

简单安装教程 course.rs/first-try/i…

Cargo

Cargo 是Rust的包管理器但是不局限于包管理器,是Rust包括编译,测试运行及部署

创建及初始化

cargo通过关键字new进行项目的创建

cargo new <项目名>

通过命令,cargo会为我们创建一个指定名称的文件夹,并将自动初始化基本的项目结构、配置文件,及git初始化文件。。。。

$ tree
.
├── .git
├── .gitignore
├── Cargo.toml
└── src
    └── main.rs

项目运行及编译

我们可以通过命令run运行项目,而run是一个组合命令,内部包含build动作,下面两个命令基本相同

// 这里与下方命令相同
cargo run <project>

cargo build <project>
./target/debug/<project>

默认使用build命令生成的是一个debug文件,内部包含了日志及一些杂七杂八的内容,在debug模式下,编译速度会很快,上帝打开一扇门也会顺手关上一扇窗,在debug模式下编译速度快,那么运行速度就会相对变得慢一些

通过--release 后缀可以编译一个正式的包,使用后会在target包下生成一个release的包存放正式的文件

cargo run --release
cargo build --release

Cargo Check

想象一种情况假如你现在是一个十分庞大的开源项目维护者(当然我是说想象),你现在想要验证你的项目中是否有明显的语法错误,使用run或者build 你将花30分钟或者更长。想想都很恐怖,最恐怖的是第一次run后有错误,然后再来一次还有。。。。。。 好消息Rust为了减少这种恐怖的事情为我们提供了 cargo check命令 使用办法也是直接输入 cargo check 可以快速验证项目正确性,在项目变得复杂及庞大的时候可以迅速验证减少垃圾时间

Cargo.toml 和 Cargo.lock

使用过nodejs就会很熟悉,一个是项目数据描述文件,一个是项目依赖详细清单

什么情况下该把 Cargo.lock 上传到 git 仓库里?很简单,当你的项目是一个可运行的程序时,就上传 Cargo.lock,如果是一个依赖库项目,那么请把它添加到 .gitignore 中。

cargo.toml 文件

[package] 
name = "world_hello"  // 项目名
version = "0.1.0"  // 版本号
edition = "2021"   // Rust大版本

导入依赖的方式

[dependencies] 
rand = "0.3" // 官方仓库依赖的方式 只需要提供版本号
hammer = { version = "0.5.0"} //官方仓库依赖的方式 只需要提供版本号(另一种形式)
color = { git = "https://github.com/bjz/color-rs" }  // 基于项目源代码的 git 仓库地址,通过 URL 来描述
geometry = { path = "crates/geometry" } // - 基于本地项目的绝对路径或者相对路径,通过类 Unix 模式的路径来描述

依赖下载慢

创建rust项目

cargo new <项目名>

默认创建的是bin项目

cargo创建分为bin项目和lib项目,前者为可执行项目后者是依赖库项目

cargo new <项目名> --lib


错误预警:

error: linker link.exe not found | = note: program not found

当第一次运行rust很大概率会遇到第一个报错

解决方案分两种

  1. 先安装 Microsoft C++ Build Tools,勾选安装 C++ 环境即可。安装时可自行修改缓存路径与安装路径,避免占用过多 C 盘空间。安装完成后,Rust 所需的 msvc 命令行程序需要手动添加到环境变量中,否则安装 Rust 时 rustup-init 会提示未安装 Microsoft C++ Build Tools,其位于:%Visual Studio 安装位置%\VC\Tools\MSVC%version%\bin\Hostx64\x64(请自行替换其中的 %Visual Studio 安装位置%、%version% 字段)下。

如果你不想这么做,可以选择安装 Microsoft C++ Build Tools 新增的“定制”终端 Developer Command Prompt for %Visual Studio version%Developer PowerShell for %Visual Studio version%,在其中运行 rustup-init.exe

准备好 C++ 环境后开始安装 Rust:

RUSTUP-INIT 下载系统相对应的 Rust 安装程序,一路默认即可。

 PS C:\Users\Hehongyuan> rustup-init.exe
 ......
 Current installation options:
 ​
    default host triple: x86_64-pc-windows-msvc
      default toolchain: stable (default)
                profile: default
   modify PATH variable: yes
 ​
 1) Proceed with installation (default)
 2) Customize installation
 3) Cancel installation
  1. x86_64-pc-windows-gnu

    相比于 MSVC 版本来说,GNU 版本具有更轻量,更靠近 Linux 的优势。

    首先,根据 MSYS2 官网 配置 MSYS。

    若您觉得下载太慢,可以试试由 Caviar-X 提供的 代理

    在安装 mingw-toolchain 后,请将 %MSYS 安装路径%\mingw64\bin 添加到系统变量 PATH 中。

    配置好后,在 MSYS 中输入下面的命令来安装 rustup。

     $ curl https://sh.rustup.rs -sSf | sh
    

    之后,根据以下输出进行配置。

     Current installation options:
     ​
        default host triple: x86_64-pc-windows-msvc
          default toolchain: stable (default)
                    profile: default
       modify PATH variable: yes
     ​
     1) Proceed with installation (default)
     2) Customize installation
     3) Cancel installation
     >2
     ​
     I'm going to ask you the value of each of these installation options.
     You may simply press the Enter key to leave unchanged.
     ​
     Default host triple? [x86_64-pc-windows-msvc]
     x86_64-pc-windows-gnu
     ​
     Default toolchain? (stable/beta/nightly/none) [stable]
     stable
     ​
     Profile (which tools and data to install)? (minimal/default/complete) [default]
     complete
     ​
     Modify PATH variable? (Y/n)
     Y
     ​
     Current installation options:
     ​
        default host triple: x86_64-pc-windows-gnu
          default toolchain: stable
                    profile: complete
       modify PATH variable: yes
     ​
     1) Proceed with installation (default)
     2) Customize installation
     3) Cancel installation
     >
    

    再之后,按下 1,等待。完成后,您就已经安装了 Rust 和 rustup

  2. 当然也可以使用更简单的方式

    rustup toolchain install stable-x86_64-pc-windows-gnu

    rustup default stable-x86_64-pc-windows-gnu

运行及编译

运行命令

cargo run

这个命令合并了两个编译和执行两个命令

cargo build

编译rust程序,将会在taget目录下生成一个debug包,这个包使用的是debug模式,这个模式下编译速度将相当快,相对的运行速度会变慢

cargo build --release

添加--release将获得高性能release程序

cargo check

rust 的编译数据没有这么快,使用cargo check 可以有效的避免浪费人生,帮你检查是否可以编译通过

目录解读

image-20240423163334684.png

  • .git && .gitignore 不解释
  • src 源代码目录 rust还是挺在意目录结构的不像go创建项目后只有一个go.mod连main.go都需要自己手动
  • target 编译目录
  • Cargo.toml 依赖库文件 就像Maven的xml
  • Cargo.lock 这个我猜是和nodejs学习的

Cargo.toml解释

[package]

name = "world_hello"

version = "0.1.0"

edition = "2021"

name 字段定义了项目名称,version 字段定义当前版本,新项目默认是 0.1.0edition 字段定义了我们使用的 Rust 大版本。所以使用的是 Rust edition 2021 大版本