首页 > 其他 > 详细

cargo

时间:2020-10-23 17:42:58      阅读:35      评论:0      收藏:0      [点我收藏+]
cargo --version

[root@bogon ~]# cargo --version
cargo 1.47.0 (f3c7e066a 2020-08-28)
You have new mail in /var/spool/mail/root
[root@bogon ~]# 

 

 

使用 cargo 创建并构建一个完整的二进制可执行程序项目

 

[root@bogon data2]# mkdir rust
[root@bogon data2]# cd rust/
[root@bogon rust]# ls
[root@bogon rust]# cargo new app1 --bin
     Created binary (application) `app1` package
[root@bogon rust]# ls
app1
[root@bogon rust]# tree -D app1/
app1/
|-- [Oct 23 16:33]  Cargo.toml
`-- [Oct 23 16:33]  src
    `-- [Oct 23 16:33]  main.rs

1 directory, 2 files
[root@bogon rust]# 

 

通过上面的篇幅可知:cargo new 命令用于创建一个新项目,--bin 选项用于指示当前项目是一个 可执行的二进制程序 项目。

在 Cargo 中,通常把一个项目称之为 crates 

Rust 官方为 cargo 包管理器提供了中央托管网站。网站地址为 https://crates.io/。 我们可以从该网站上找到一些我们需要的第三方库或者一些有用的第三方项目。

 

 

[root@bogon app1]# ls
Cargo.toml  src
[root@bogon app1]# cat Cargo.toml 
[package]
name = "app1"
version = "0.1.0"
authors = ["xxx"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
[root@bogon app1]# 

 

二、添加项目需要的第三方依赖库

我们的 猜数字 游戏需要生成 随机数。 但是 Rust 语言核心和 Rust 标准库都没有提供生成随机数的的方法或结构体。

我们只能借助于 https://crates.io/ 上其它开发者提供的第三方库或 crates

我们可以打开网站 crates.io 并在顶部的搜索中输入 rand 然后回车来查找第三方随机数生成库。

从搜索的结果来看,由很多和随机数生成的相关库,不过我们只使用 rand

点击搜索结果中的 rand 会跳转到 https://crates.io/crates/rand

下图是我们需要的 随机数生成库 rand 的基本信息截图。

技术分享图片

了解了 rand 的基本信息后,我们就可以修改 Cargo.toml 添加 rand 依赖了。

准确的说就是在 [dependencies] 节中添加 rand = "0.6.5"

编辑完成后的文件内容如下

 

[root@bogon app1]# cat Cargo.toml 
[package]
name = "app1"
version = "0.1.0"
authors = [" "]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.6.5"

三、 先预编译项目

对于 Rust / C / C++ 这种静态语言,先预编译一下应该是一种习惯。

因为预编译的时候会告诉我们项目配置是否正确,同时会下载第三方库,这样就可以 自动提示 了。

使用 cargo 创建的项目,我们可以在终端里输入 cargo build 来预编译下项目。

输出一般如下

[root@bogon app1]# cargo build 
    Updating crates.io index
  Downloaded rand_chacha v0.1.1
  Downloaded rand_hc v0.1.0
  Downloaded rand_xorshift v0.1.1
  Downloaded rand_jitter v0.1.4
  Downloaded rand_isaac v0.1.1
  Downloaded autocfg v0.1.7
  Downloaded rand_core v0.3.1
  Downloaded rand_os v0.1.3
  Downloaded rand_pcg v0.1.2
  Downloaded rand v0.6.5
  Downloaded rand_core v0.4.2
  Downloaded 11 crates (249.3 KB) in 12.07s
   Compiling autocfg v0.1.7
   Compiling rand_core v0.4.2
   Compiling libc v0.2.79
   Compiling rand_core v0.3.1
   Compiling rand_jitter v0.1.4
   Compiling rand_isaac v0.1.1
   Compiling rand_xorshift v0.1.1
   Compiling rand_hc v0.1.0
   Compiling rand_pcg v0.1.2
   Compiling rand_chacha v0.1.1
   Compiling rand v0.6.5
   Compiling rand_os v0.1.3
   Compiling app1 v0.1.0 (/data2/rust/app1)
    Finished dev [unoptimized + debuginfo] target(s) in 1m 15s
[root@bogon app1]# 
[root@bogon app1]# tree -D
.
|-- [Oct 23 16:39]  Cargo.lock
|-- [Oct 23 16:37]  Cargo.toml
|-- [Oct 23 16:33]  src
|   `-- [Oct 23 16:33]  main.rs
`-- [Oct 23 16:39]  target
    |-- [Oct 23 16:39]  CACHEDIR.TAG
    `-- [Oct 23 16:39]  debug
        |-- [Oct 23 16:39]  app1
        |-- [Oct 23 16:39]  app1.d
        |-- [Oct 23 16:39]  build
        |   |-- [Oct 23 16:39]  libc-48c197210439ab2b
        |   |   |-- [Oct 23 16:39]  build-script-build
        |   |   |-- [Oct 23 16:39]  build_script_build-48c197210439ab2b
        |   |   `-- [Oct 23 16:39]  build_script_build-48c197210439ab2b.d
        |   |-- [Oct 23 16:39]  libc-88ca55d88ca3404d
        |   |   |-- [Oct 23 16:39]  invoked.timestamp
        |   |   |-- [Oct 23 16:39]  out
        |   |   |-- [Oct 23 16:39]  output
        |   |   |-- [Oct 23 16:39]  root-output
        |   |   `-- [Oct 23 16:39]  stderr
        |   |-- [Oct 23 16:39]  rand-17b43e1f4f338ea3
        |   |   |-- [Oct 23 16:39]  invoked.timestamp
        |   |   |-- [Oct 23 16:39]  out
        |   |   |   `-- [Oct 23 16:39]  probe0.ll
        |   |   |-- [Oct 23 16:39]  output
        |   |   |-- [Oct 23 16:39]  root-output
        |   |   `-- [Oct 23 16:39]  stderr
        |   |-- [Oct 23 16:39]  rand-89f79ead84a8ac84
        |   |   |-- [Oct 23 16:39]  build-script-build
        |   |   |-- [Oct 23 16:39]  build_script_build-89f79ead84a8ac84
        |   |   `-- [Oct 23 16:39]  build_script_build-89f79ead84a8ac84.d
        |   |-- [Oct 23 16:39]  rand_chacha-94f70950fc64093e
        |   |   |-- [Oct 23 16:39]  build-script-build
        |   |   |-- [Oct 23 16:39]  build_script_build-94f70950fc64093e
        |   |   `-- [Oct 23 16:39]  build_script_build-94f70950fc64093e.d
        |   |-- [Oct 23 16:39]  rand_chacha-973a4c4c879b0b17
        |   |   |-- [Oct 23 16:39]  invoked.timestamp
        |   |   |-- [Oct 23 16:39]  out
        |   |   |   `-- [Oct 23 16:39]  probe0.ll
        |   |   |-- [Oct 23 16:39]  output
        |   |   |-- [Oct 23 16:39]  root-output
        |   |   `-- [Oct 23 16:39]  stderr
        |   |-- [Oct 23 16:39]  rand_pcg-7736aaaf3fd4aa4e
        |   |   |-- [Oct 23 16:39]  invoked.timestamp
        |   |   |-- [Oct 23 16:39]  out
        |   |   |   `-- [Oct 23 16:39]  probe0.ll
        |   |   |-- [Oct 23 16:39]  output
        |   |   |-- [Oct 23 16:39]  root-output
        |   |   `-- [Oct 23 16:39]  stderr
        |   `-- [Oct 23 16:39]  rand_pcg-9b895cf06b89458e
        |       |-- [Oct 23 16:39]  build-script-build
        |       |-- [Oct 23 16:39]  build_script_build-9b895cf06b89458e
        |       `-- [Oct 23 16:39]  build_script_build-9b895cf06b89458e.d
        |-- [Oct 23 16:39]  deps
        |   |-- [Oct 23 16:39]  app1-edce79195f703d0b
        |   |-- [Oct 23 16:39]  app1-edce79195f703d0b.d
        |   |-- [Oct 23 16:39]  autocfg-6a5bbd19e854ddb5.d
        |   |-- [Oct 23 16:39]  libautocfg-6a5bbd19e854ddb5.rlib
        |   |-- [Oct 23 16:39]  libautocfg-6a5bbd19e854ddb5.rmeta
        |   |-- [Oct 23 16:39]  libc-fc4c843f6fca6e5f.d
        |   |-- [Oct 23 16:39]  liblibc-fc4c843f6fca6e5f.rlib
        |   |-- [Oct 23 16:39]  liblibc-fc4c843f6fca6e5f.rmeta
        |   |-- [Oct 23 16:39]  librand-99699316ef438a28.rlib
        |   |-- [Oct 23 16:39]  librand-99699316ef438a28.rmeta
        |   |-- [Oct 23 16:39]  librand_chacha-87ecff9cee2e0560.rlib
        |   |-- [Oct 23 16:39]  librand_chacha-87ecff9cee2e0560.rmeta
        |   |-- [Oct 23 16:39]  librand_core-5e310b0d7db91be2.rlib
        |   |-- [Oct 23 16:39]  librand_core-5e310b0d7db91be2.rmeta
        |   |-- [Oct 23 16:39]  librand_core-ce047d2d5f752996.rlib
        |   |-- [Oct 23 16:39]  librand_core-ce047d2d5f752996.rmeta
        |   |-- [Oct 23 16:39]  librand_hc-3cf4e841d9674c6e.rlib
        |   |-- [Oct 23 16:39]  librand_hc-3cf4e841d9674c6e.rmeta
        |   |-- [Oct 23 16:39]  librand_isaac-77b887c1d366684f.rlib
        |   |-- [Oct 23 16:39]  librand_isaac-77b887c1d366684f.rmeta
        |   |-- [Oct 23 16:39]  librand_jitter-44c930d5cf687a04.rlib
        |   |-- [Oct 23 16:39]  librand_jitter-44c930d5cf687a04.rmeta
        |   |-- [Oct 23 16:39]  librand_os-da217c76741d47d6.rlib
        |   |-- [Oct 23 16:39]  librand_os-da217c76741d47d6.rmeta
        |   |-- [Oct 23 16:39]  librand_pcg-a6bd8f47682b15bb.rlib
        |   |-- [Oct 23 16:39]  librand_pcg-a6bd8f47682b15bb.rmeta
        |   |-- [Oct 23 16:39]  librand_xorshift-306bdc86c1f9e8a3.rlib
        |   |-- [Oct 23 16:39]  librand_xorshift-306bdc86c1f9e8a3.rmeta
        |   |-- [Oct 23 16:39]  rand-99699316ef438a28.d
        |   |-- [Oct 23 16:39]  rand_chacha-87ecff9cee2e0560.d
        |   |-- [Oct 23 16:39]  rand_core-5e310b0d7db91be2.d
        |   |-- [Oct 23 16:39]  rand_core-ce047d2d5f752996.d
        |   |-- [Oct 23 16:39]  rand_hc-3cf4e841d9674c6e.d
        |   |-- [Oct 23 16:39]  rand_isaac-77b887c1d366684f.d
        |   |-- [Oct 23 16:39]  rand_jitter-44c930d5cf687a04.d
        |   |-- [Oct 23 16:39]  rand_os-da217c76741d47d6.d
        |   |-- [Oct 23 16:39]  rand_pcg-a6bd8f47682b15bb.d
        |   `-- [Oct 23 16:39]  rand_xorshift-306bdc86c1f9e8a3.d
        |-- [Oct 23 16:39]  examples
        `-- [Oct 23 16:39]  incremental
            `-- [Oct 23 16:39]  app1-1froeqw3vt1uy
                |-- [Oct 23 16:39]  s-fsdf9f4vs4-nluwcr-3vd0m9p0cpi2j
                |   |-- [Oct 23 16:39]  1capbncndx529ia9.o
                |   |-- [Oct 23 16:39]  2xlne85efcng7orr.o
                |   |-- [Oct 23 16:39]  4e6t7tsy2f7rnu07.o
                |   |-- [Oct 23 16:39]  4l3obphrj6hgkslq.o
                |   |-- [Oct 23 16:39]  4tchaufqhlp2f30q.o
                |   |-- [Oct 23 16:39]  4tm5eg7kw2ktz7ht.o
                |   |-- [Oct 23 16:39]  57s758bcpv2w4hik.o
                |   |-- [Oct 23 16:39]  dep-graph.bin
                |   |-- [Oct 23 16:39]  m8yt13xiixwxcbb.o
                |   |-- [Oct 23 16:39]  query-cache.bin
                |   `-- [Oct 23 16:39]  work-products.bin
                `-- [Oct 23 16:39]  s-fsdf9f4vs4-nluwcr.lock

21 directories, 87 files
[root@bogon app1]# ls
Cargo.lock  Cargo.toml  src  target
[root@bogon app1]# tree -D -L 2
.
|-- [Oct 23 16:39]  Cargo.lock
|-- [Oct 23 16:37]  Cargo.toml
|-- [Oct 23 16:33]  src
|   `-- [Oct 23 16:33]  main.rs
`-- [Oct 23 16:39]  target
    |-- [Oct 23 16:39]  CACHEDIR.TAG
    `-- [Oct 23 16:39]  debug

3 directories, 4 files
[root@bogon app1]# 
[root@bogon app1]# cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/app1`
Hello, world!
[root@bogon app1]# 

 

 

[root@bogon app1]# cat src/main.rs 
extern crate rand;
use rand::random;
fn main() {
    let correct:u8 = random();
    println!("correct value is {}",correct);
    println!("Hello, world!");
}
[root@bogon app1]# 

 

 

[root@bogon app1]# cargo build 
   Compiling app1 v0.1.0 (/data2/rust/app1)
    Finished dev [unoptimized + debuginfo] target(s) in 0.57s
[root@bogon app1]# cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/app1`
correct value is 141
Hello, world!
[root@bogon app1]# 

 

 

 

 

cargo

原文:https://www.cnblogs.com/dream397/p/13864951.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!