1.2 rust cargo

2023-06-14,,

cargo是rust的编译与打包工具,可将rust打包成为一个可执行性文件。生成的可执行性文件不能跨系统的大版本,比如在linux7上打包,那么程序无法在linux6上执行。

# cargo new hello_cargo
Created binary (application) `hello_cargo` package
# cd hello_cargo
# ls
Cargo.toml src
# ll -a
total
drwxr-xr-x root root Jan : .
drwxr-xr-x root root Jan : ..
-rw-r--r-- root root Jan : Cargo.toml
drwxr-xr-x root root Jan : .git
-rw-r--r-- root root Jan : .gitignore
drwxr-xr-x root root Jan : src
# cat Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["tanpf <itoracle@163.com>"]
edition = "" [dependencies]
# cd src
# ls
main.rs
# cat main.rs
fn main() {
println!("Hello, world!");
}
[root@itoracle src]# cargo build
Compiling hello_cargo v0.1.0 (/usr/local/automng/src/rust/test/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in .05s
[root@itoracle src]# ll
total
-rw-r--r-- root root Jan : main.rs
[root@itoracle hello_cargo]# ./target/debug/hello_cargo
Hello, world!
[root@itoracle hello_cargo]# cargo run
Finished dev [unoptimized + debuginfo] target(s) in .01s
Running `target/debug/hello_cargo`
Hello, world!
[root@itoracle hello_cargo]#
[root@itoracle hello_cargo]#
[root@itoracle hello_cargo]# vim src/main.rs fn main() {
println!("Hello, world!");
println!("过年回家的火车票,为啥每次都抢不到呢");
}

如果修改了代码,cargo run会先编译再运行

[root@itoracle hello_cargo]# cargo run
Compiling hello_cargo v0.1.0 (/usr/local/automng/src/rust/test/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in .46s
Running `target/debug/hello_cargo`
Hello, world!
过年回家的火车票,为啥每次都抢不到呢

cargo check进行代码的编译,但不生成可执行文件,速度比cargo build快很多,当代码比较多时,可以先check

[root@itoracle hello_cargo]# cargo check
Checking hello_cargo v0.1.0 (/usr/local/automng/src/rust/test/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in .13s

1.2 rust cargo的相关教程结束。

《1.2 rust cargo.doc》

下载本文的Word格式文档,以方便收藏与打印。