codecamp

Cargo 的第一步

Cargo 的第一步

要使用 Cargo 启动新项目,请使用cargo new:

$ cargo new hello_world --bin

我们传递--bin,是因为我们正在制作一个二进制程序(默认): 如果我们正在创建一个库(lib),我们就会把传递--lib.

让我们来看看 Cargo 为我们带来了什么:

$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
    └── main.rs
1 directory, 2 files

这就是我们开始所需要的一切。首先,让我们看看Cargo.toml:

[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"

[dependencies]

这被称为一个manifest元清单,它包含了 Cargo 编译项目所需的所有元数据.

那src/main.rs有啥:

fn main() {
    println!("Hello, world!");
}

Cargo 为我们创造了一个"hello_world".我们来编译它:

$ cargo build
   Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)

然后运行它:

$ ./target/debug/hello_world
Hello, world!

我们也可以直接使用cargo run,它会自行编译,然后运行它, 一步到位:

$ cargo run
     Fresh hello_world v0.1.0 (file:///path/to/project/hello_world)
   Running `target/hello_world`
Hello, world!


Cargo 安装
Cargo 为什么存在
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Cargo 常见问题

Cargo 附录:词汇表

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }