Rust源码编译

Rust源码编译

rustc编译hello.rs

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

cargo

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

➜  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创建项目

# 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创建项目后,默认的文件目录结构:

basic-01 #[项目顶级目录]
|-- Cargo.toml
`-- src
    `-- main.rs

cargo build 构建项目

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

➜  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。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Erice_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值