Rust的包管理器Cargo

来源:互联网 发布:mac os lion无法升级 编辑:程序博客网 时间:2024/05/13 03:38
安装

     安装Cargo最简单的方法是使用rustup脚本获得:
     
     $ curl -sS https://static.rust-lang.org/rustup.sh | sudo bash

     你将会获得最新版本的Rust和最新版本的Cargo。你需要每天运行一次该脚本来获取最新升级。

     如果你使用的是Windows,直接下载最新版的32位(Rust和Cargo)或64位(Rust和Cargo)安装包。

     或者,你可以从源码构建Cargo。

     让我们开始吧

     用Cargo开始一个新项目,使用 cargo new:

     $ cargo new hello_world --bin

     我们传递--bin是因为我们制作二进制程序:如果我们只做一个库,我们将不会传递--bin。

     查看一下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>"]


     这被称为“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手册。


转载请注明出处:http://blog.csdn.net/ucan23/article/details/45667187

     
1 0