Rust源码编译

Rust源码编译

rustc编译hello.rs

  • Rust程序源代码文件后缀名为.rs
  • 程序文件命名规范为小写字母加下划线,比如hello_world.rs
rust 复制代码
// hello.rs
fn main() {
    println!("hello world");
}
// fn 表示函数
// main 表示函数名,()内是参数
// {}内是函数体代码
// 代码以分号结束
// 程序代码缩进是4个空格
  • rustc hello.rs 执行后在当前目录生成hello可执行程序

cargo

cargo 是Rust的构建系统和包管理工具,能够构建项目代码、下载依赖库、构建库,在安装Rust时会自动安装cargo , 判断cargo 是否正确安装的方法就是在命令行中输入下述命令:

shell 复制代码
➜  hello_rust git:(master) ✗ cargo -V
cargo 1.71.1 (7f1d04c00 2023-07-29)
➜  hello_rust git:(master) ✗ cargo --version
cargo 1.71.1 (7f1d04c00 2023-07-29)

使用cargo创建项目

sh 复制代码
# cargo new 项目名称
cargo new hello_cargo
# 项目名称不能以数字开始
➜  hello_rust git:(master) ✗ cargo new 01-basic
error: the name `01-basic` cannot be used as a package name, the name cannot start with a digit
If you need a package name to not match the directory name, consider using --name flag.
If you need a binary with the name "01-basic", use a valid package name, and set the binary name to be different from the package. This can be done by setting the binary filename to `src/bin/01-basic.rs` or change the name in Cargo.toml with:

    [[bin]]
    name = "01-basic"
    path = "src/main.rs"
#  cargo new basic-01 创建项目成功
➜  02-cargo_build cargo new basic-01
     Created binary (application) `basic-01` package

cargo创建项目后,默认的文件目录结构:

shell 复制代码
basic-01 #[项目顶级目录]
|-- Cargo.toml
`-- src
    `-- main.rs

cargo build 构建项目

cargo build 命令自动进行编译项目,可执行程序名称和项目名称一致,basic-01

bash 复制代码
➜  basic-01 git:(master) ✗ cargo build
   Compiling basic-01 v0.1.0 (/root/rust/02-cargo_build/basic-01)
    Finished dev [unoptimized + debuginfo] target(s) in 0.29s
➜  basic-01 git:(master) ✗ ls
Cargo.lock  Cargo.toml  src  target
➜  basic-01 git:(master) ✗ tree target
target
|-- CACHEDIR.TAG
`-- debug
    |-- basic-01
    |-- basic-01.d
    |-- build
    |-- deps
    |   |-- basic_01-e75f9a220d3ad071
    |   `-- basic_01-e75f9a220d3ad071.d
    |-- examples
    `-- incremental
        `-- basic_01-2kh2yg4h6jcag
            |-- s-go2vdsztz6-al0tod-13wjiwhvtbk4c98mo163x460g
            |   |-- 1i85aafl0k1zfwu0.o
            |   |-- 1j4di08lgnzunw6s.o
            |   |-- 1kgk1cvp2qpwyh2b.o
            |   |-- 1nzi9licjx2a4suv.o
            |   |-- 3fohwnhiheayk2p7.o
            |   |-- 4tz67ju1du877p49.o
            |   |-- dep-graph.bin
            |   |-- query-cache.bin
            |   `-- work-products.bin
            `-- s-go2vdsztz6-al0tod.lock

cargo run 构建和运行项目

cargo run 命令是对代码进行编译和执行,将编译和执行这两步合成一步,如果项目之前编译成功过并且源代码没有变化,那么再次运行cargo run 命令时就会直接运行二进制文件,而不会重复进行编译。

cargo check

该命令可以用来检查代码,确保能够通过编译,但是不产生任何可执行文件,该命令比cargo build 命令快的多,在编写代码的时候可以连续反复的使用cargo check 检查代码提高开发效率。

构建Release版本

如果程序开发完成准备正式发布给用户时,就需要使用cargo build --release ,该命令会进行优化,优化后代码运行的更快,但是编译的时间更长。

使用该命令构建的可执行文件会在targe/release目录下,默认构建为debug。

相关推荐
ignativs amor16 分钟前
Python redis 安装和使用介绍
开发语言·redis·python
你可以自己看19 分钟前
Python在数据科学与机器学习中的应用
开发语言·python·机器学习
Jacky-YY19 分钟前
教你如何在Java中操作Redis
java·开发语言·redis
JOJO___21 分钟前
Spring AOP 总结
java·后端·spring·java-ee
大白的编程日记.1 小时前
【C++笔记】C++编译器拷贝优化和内存管理
java·开发语言·c++·笔记
JovaZou1 小时前
[Python学习日记-26] Python 中的文件操作
开发语言·python·学习
¥ 多多¥3 小时前
数据结构:内存的使用
linux·c语言·开发语言·数据结构
EPSDA3 小时前
Java的IO流(二)
java·开发语言
vitobo4 小时前
Java的JDBC编程
java·开发语言
呆萌小新@渊洁4 小时前
后端接收数组,集合类数据
android·java·开发语言