您的位置:首页 > 编程语言 > Go语言

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

2016-03-17 18:32 736 查看
cargo是Rust内置的项目管理工具。用于Rust 项目的创建、编译、运行,同时对项目的依赖进行管理,自动判断使用的第三方依赖库,进行下载和版本升级。

一、查看 cargo 版本

安装Rust之后,可以使用

cargo --version


查看cargo的版本信息。

$ cargo --version
cargo 0.8.0-nightly (28a0cbb 2016-01-17)


二、创建新项目

1、使用 “ new 项目名称“ 创建新项目

cargo new hello_world


cargo new 默认创建的是library项目;

2、如果要创建可运行的 Rust 项目,需要添加–bin 参数

cargo new hello_world --bin


3、项目结构

创建项目后,当前路径下创建以项目名称为名字的文件夹作为项目的根目录。

在项目的根目录下,包括一个cargo的配置文件和一个src目录。

A. cargo配置文件的名字为 Cargo.toml,文件内容如下:

[package]

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


B. src目录下,包含一个Rust文件。

如果是可运行的项目,这个文件的名字为 main.rs,内容为:

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


如果是一个库项目,这个文件的名字为 lib.rs ,内容为:

#[cfg(test)]
mod test {
#[test]
fn it_works() {
}
}


二、编译项目

$ cargo build


$cargo build
Compiling hello_world v0.0.1 (file:///Users/teamlet/develop/rust-projects/hello_world)


三、运行项目

$cargo run


$ cargo run
Running `target/debug/hello_world`
Hello,world!


四、获取帮助

可以运行

cargo --help


获得帮助菜单;

或者carg + 命令名 - - help 获得更详细的帮助信息

cargo new --help
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: