rst002_rust cargo

Posted perfei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rst002_rust cargo相关的知识,希望对你有一定的参考价值。

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 24
drwxr-xr-x 4 root root 4096 Jan 15 18:16 .
drwxr-xr-x 3 root root 4096 Jan 15 18:16 ..
-rw-r--r-- 1 root root  121 Jan 15 18:16 Cargo.toml
drwxr-xr-x 6 root root 4096 Jan 15 18:16 .git
-rw-r--r-- 1 root root   19 Jan 15 18:16 .gitignore
drwxr-xr-x 2 root root 4096 Jan 15 18:16 src
# cat Cargo.toml 
[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["tanpf <[email protected]>"]
edition = "2018"

[dependencies]

 

# cd src
# ls
main.rs
# cat main.rs 
fn main() {
    println!("Hello, world!");
}
[[email protected] src]# cargo build
   Compiling hello_cargo v0.1.0 (/usr/local/automng/src/rust/test/hello_cargo)                                                                                              
    Finished dev [unoptimized + debuginfo] target(s) in 3.05s                                                                                                               
[[email protected] src]# ll
total 4
-rw-r--r-- 1 root root 45 Jan 15 18:16 main.rs
[[email protected] hello_cargo]# ./target/debug/hello_cargo 
Hello, world!
[[email protected] hello_cargo]# cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s                                                                                                               
     Running `target/debug/hello_cargo`
Hello, world!
[[email protected] hello_cargo]# 
[[email protected] hello_cargo]# 
[[email protected] hello_cargo]# vim src/main.rs 

fn main() {
    println!("Hello, world!");
    println!("过年回家的火车票,为啥每次都抢不到呢");
}

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

[[email protected] 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 0.46s                                                                                                               
     Running `target/debug/hello_cargo`
Hello, world!
过年回家的火车票,为啥每次都抢不到呢

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

[[email protected] 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 0.13s 

 

以上是关于rst002_rust cargo的主要内容,如果未能解决你的问题,请参考以下文章

Rust: Cargo 使用本地 crate

Rust 中项目构建管理工具 Cargo简单介绍

rust cargo 一些方便的三方cargo 子命令扩展

Rust Rustup cargo配置国内镜像源

Rust 之 cargo(项目构建和包管理工具)

Rust中的Cargo工作空间实践