1.使用cargo创建项目,会在当前目录下生成hello_cargo文件夹cargo new hello_cargo --bin
2.使用Goland打开项目hello_cargo,目录如下。其中Cargo.toml中[package]定义的是关于项目的一些信息,[dependencies]中定义的是外部依赖
3.引入外部包rand,rand
在Cargo.toml文件[dependencies]下加入rand = "0.3.14"
4.cargo build将会从远端下载rand包,包括rand的依赖包
5.使用外部包1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21extern crate rand; //表明rand是外部依赖
use std::io;
use rand::Rng;
fn main() {
    println!("Guess the number!");
    let secret_number = rand::thread_rng().gen_range(1, 101);
    println!("The secret number is:{}", secret_number);
    println!("Please input your guess.");
    let mut guess = String::new();
    io::stdin().read_line(&mut guess)
        .expect("Failed to read line");
    println!("You guessed:{}", guess);
}
6.编译执行
cargo相关命令
| command | desc | 
|---|---|
| rustup doc | 打开本地文档 | 
| rustc main.rs | 编译 | 
| rustup update | 更新Rust工具链和rustup | 
| cargo new | 新建一个可执行应用 | 
| cargo build | 生成可执行文件 | 
| cargo update | 更新外部依赖,并更新Cargo.lock文件 | 
| cargo build —release | 生成可发布的执行文件,Rust有做一些优化 | 
| cargo run | 如果存在目标文件,且Rust认为代码没有改变,则直接执行可执行文件;如果源码改变或可执行文件不存在,则编译后再执行 | 
| cargo check | 检查代码是否能正确编译,速度当然比cargo build,因为不产生可执行文件,编码中用于快速检查代码是否有问题 | 
使用过程中遇到的问题:
Caused by:
  error authenticating: failed connecting agent; class=Ssh (23)
解决方法:将~/.gitconfig中的两行内容注释,Windows下路径为C:\Users\Administrator\1
2#[url "git@github.com:"]
# 	insteadOf = https://github.com/
Ref:
1.The Rust Programming Language
2.https://github.com/rust-lang/cargo/issues/3381