Rust的包管理器Cargo

安装

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

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

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

     或者,你可以从源码构建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手册



     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值