美文网首页
Rust 入门

Rust 入门

作者: LV大树 | 来源:发表于2024-03-31 01:15 被阅读0次

Rust 第一天猜数字



use core::num;
use std::io;
use rand::Rng;
use std::cmp::Ordering;

fn main() {
    println!("Hello, world!");
    println!("Guess the  number!");
    let secret_number = rand::thread_rng().gen_range(1..=100);

    loop {
    println!("pleas input your guess");
   let mut guess = String::new();
   io::stdin()
       .read_line(&mut guess)
       .expect("Failed to read line");
   
    let guess: u32 = match guess.trim().parse() {   //.expect("please type a number") {
        Ok(num) => num,
        Err(_) => continue,
    };

     println!("the secret number is {secret_number}"); 
     println!("you guess: {guess}");

    match guess.cmp(&secret_number) {
    Ordering::Less => {println!("Too small")},
    Ordering::Greater => {println!("Too big!")},
    Ordering::Equal => {
        println!("You win! ");
        break;
        }
    }
   }
}

相关文章

  • Rust 问答之 Cargo 是什么

    Cargo:Rust 的构建工具和包管理器 文章标题来自于 Rust 官网: 入门 - Rust 程序设计语言 在...

  • Rust CLI应用程序的入门模板:rust-starter

    rust-starter是一个创建Rust CLI应用程序的入门模板。 特性 Clap[https://githu...

  • Rust 入门 (Rust Rocks)

    缘起 实践出真知快速获取澄清概念OwnershipMoveReferenceMutable reference解释...

  • Rust语言入门

    一、简介 Rust是Mozilla公司推出的一门全新的编程语言,1.0版本于2015年5月15日正式对外发布。作为...

  • Rust 入门 - HashMap

    导入库 使用 vector 和 hashmap 数据都在堆上 用队伍列表和分数列表创建哈希 map 哈希 map ...

  • Rust 入门 - 方法

    函数/方法 无返回值 有返回值 语句不会返回值,表达式会返回值

  • Rust 入门 - Slice

    Slice 另一个没有所有权的数据类型是 slice。slice 允许你引用集合中一段连续的元素序列,而不用引用整...

  • Rust 入门 - 类型

    类型 布尔类型 char类型, 在 Rust 中 char 是32位的 数字类型i8, i16, i32, i64...

  • Rust 入门 - Cargo

    cargo 创建工程 检查语法 编译 编译并运行

  • Rust 入门 - Mod

    绝对路径(absolute path)从 crate 根开始,以 crate 名或者字面值 crate 开头。相对...

网友评论

      本文标题:Rust 入门

      本文链接:https://www.haomeiwen.com/subject/bwpvtjtx.html