zoukankan      html  css  js  c++  java
  • rust hello world --引入第三方库或 crates

    1 hello world 的例子可参考 https://kaisery.github.io/trpl-zh-cn/  及  https://rust-by-example.budshome.com/index.html

    2、下面参考 https://www.twle.cn/c/yufei/rust/rust-basic-package-manager.html  试一下

    范例: 使用 cargo 创建并构建一个完整的二进制可执行程序项目

     参考:https://www.zhihu.com/question/462461906/answer/1915949373

    https://doc.rust-lang.org/cargo/commands/cargo-install.html

    https://rust-by-example.budshome.com/index.html

    https://kaisery.github.io/trpl-zh-cn/ 

    1  (win10系统)  在某个目录下执行:     cargo new guess-game-app  

    提示    Created binary (application) `guess-game-app` package 说明项目创建成功

     打开 https://crates.io/ 找第三方库或 crates  ,输入 rand  搜索,结果中排第一位的是:rand v0.8.4 

    3 修改刚才创建的guess-game-app目录下的Cargo.toml ,[dependencies] 节中添加 rand = "0.8.4"

    输入 cargo build来预编译项目

    显示:warning: spurious network error (2 tries remaining): failed to send request: 操作超时

    5 参考 https://www.cnblogs.com/fifolilo/p/13184622.html  Win10 修改 rust 的 crates 源为中科大源

    在 C:Users你的用户名.cargo  下,新建  config 文件(注意没有扩展名),并编辑内容如下:

    [http]
    check-revoke = false
    
    [source.crates-io]
    replace-with = 'ustc'
    
    [source.ustc]
    registry = "https://mirrors.ustc.edu.cn/crates.io-index"

    6 再次执行 cargo build 安装依赖 成功

    7 修改 guess-game-appsrcmain.rs 内容如下:

    use std::io;
    extern crate rand; // 导入当前项目下的 rand 第三方库
    
    use rand::random;
    fn get_guess() -> u8 {
       loop {
          println!("Input guess") ;
          let mut guess = String::new();
          io::stdin().read_line(&mut guess)
             .expect("could not read from stdin");
          match guess.trim().parse::<u8>(){ // 需要去除输入首尾的空白
             Ok(v) => return v,
             Err(e) => println!("could not understand input {}",e)
          }
       }
    }
    fn handle_guess(guess:u8,correct:u8)-> bool {
       if guess < correct {
          println!("Too low");
          false
    
       } else if guess> correct {
          println!("Too high");
          false
       } else {
          println!("You go it ..");
          true
       }
    }
    fn main() {
       println!("Welcome to no guessing game");
    
       let correct:u8 = random();
       println!("correct value is {}",correct);
       loop {
          let guess = get_guess();
          if handle_guess(guess,correct){
             break;
          }
       }
    }

    在 终端 中输入命令 cargo run 编译并运行我们的猜数字游戏

    看了一下,第三方库也下载到了  C:Users你的用户名.cargo  目录下

  • 相关阅读:
    集合框架
    5.异常
    接口小结
    多态(3)面向对象的三大特征之三
    面向对象编程(1)继承
    第五章博客
    《MySQL数据库》MySQL集群工具mycat安装
    《MySQL数据库》MySQL分区表
    《MySQL数据库》MySQL读写分离(Atlas)
    《Redis内存数据库》Redis数据类型和基本操作
  • 原文地址:https://www.cnblogs.com/pu369/p/15138841.html
Copyright © 2011-2022 走看看