Rust1 Getting Started Programming a Guessing Game

news/2024/7/24 7:33:53 标签: 笔记, rust

Rust学习笔记

Rust编程语言入门教程课程笔记

参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community)

Lecture 1: Getting Started

hello_world.rs

rust">fn main(){
    println!("Hello World!"); // "!" means "println!" is a rust macro
}

// compile: rustc hello_world.rs
// run: ./hello_world

hello_cargo.rs

rust">fn main() {
    println!("Hello, cargo!");
}


//new project: cargo new hello_cargo
//src: main.rs 
//cargo.toml: project metadata
//check: cargo check
//build: cargo build -> target/debug/hello_cargo
//build: cargo build --release -> target/release/hello_cargo
//run: cargo run

Lecture 2: Programming a Guessing Game

rust">use std::io; //io library
use std::cmp::Ordering; //cmp method
use rand::Rng; //rand library

fn main() {

    let secret_number = rand::thread_rng().gen_range(1..=100); //gen_range method of rand::thread_rng()
    println!("Guess the number!");
    
    loop {

        println!("Please input your guess.");

        let mut guess = String::new(); //String type is in prelude
        //guess is a mutable variable

        io::stdin() //io library
            .read_line(&mut guess) //read_line returns a value of type io::Result
            .expect("Failed to read line"); //expect method of io::Result

        //io.Result is an enum
        //io.Result has variants: Ok and Err
        //Ok: the value inside is the result of the successful operation
        //Err: the value inside is the Err infomation

        let guess:u32 = match guess.trim().parse(){//shadowing
            Ok(num) => num,
            Err(_) => continue,
        };
        

        println!("You guessed: {}", guess);

        match guess.cmp(&secret_number) { //cmp method of the type String
            //cmp method compares two values and can be called on anything that can be compared
            //cmp method returns a variant of Ordering enum
            //Ordering enum has variants: Less, Greater, Equal
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }

    }
    
    
}

http://www.niftyadmin.cn/n/5151595.html

相关文章

基于动力学模型的机械臂滑膜控制

一、滑模控制设计思路 参考资料:https://zhuanlan.zhihu.com/p/463230163(思路理解) https://blog.csdn.net/xiaohejiaoyiya/article/details/90271529(干扰的处理) 滑模控制的思路有两个关键,一个是设计…

SQL左连接实战案例

要求:用表df1和表df2的数据,得到df3 一、创建表 CREATE TABLE df1 (姓名 varchar(255) DEFAULT NULL,年龄 int DEFAULT NULL,部门 varchar(255) DEFAULT NULL,id int DEFAULT NULL );CREATE TABLE df2 (部门 varchar(255) DEFAULT NULL,年龄 int DEFAU…

量化、蒸馏、分解、剪枝

量化、蒸馏、分解和剪枝都是用于深度学习模型压缩和优化的算法。 量化是一种用于减少深度学习模型计算量和内存消耗的技术。在深度学习中,模型通常使用高精度的浮点数表示参数和激活值,但这种表示方式会占用大量的内存和计算资源。而量化技术通过降低参数…

图解Linux进程优先级

目录 1.什么是进程优先级? 2.进程优先级原理 3.查看进程优先级 4.修改进程优先级 4.1 setpriority函数原型 4.2 getpriority函数原型 4.3 sched_setscheduler函数原型 4.4 sched_getscheduler函数原型 4.5 sched_setparam函数原型 4.6 sched_getparam函数…

重大喜讯 | UMS攸信技术斩获厦门5G应用大赛三等奖!

近日,第三届厦门5G应用大赛获奖项目名单公示,攸信技术的“AI5G柔性生产缺陷检测示范线”项目脱颖而出,荣获「第三届厦门5G应用大赛三等奖」! 第三届厦门5G应用大赛获奖项目名单 本次获奖既是对攸信技术5G技术创新的鼓励&#xff0…

Android Studio布局

线性布局 水平或竖直排列子元素的布局容器 相对布局 可针对容器内每个子元素设置相对位置(相对于父容器或同级子元素的位置) 网格布局 找了下面这篇文章连接可以参考(不再赘述) GridLayout(网格布局) | 菜鸟教程 (runoob.com) …

uni-app----图片点击放大功能点击文本进行复制功能【安卓app端】

uni-app----图片点击放大功能&&点击文本进行复制功能【安卓app端】 1. 封装js文件 // 图片点击放大效果 export function getimgPreview(imgList) {uni.previewImage({indicator: "number",loop: true,urls: imgList}) }// 复制文本 export function setCl…

FMC驱动LCD

硬件简介 主控:STM32H750 LCD屏幕为16位并口屏幕 CubeMX配置 chip select: 选择起始地址块号,ADDR[27:26] Memory type: 内存类型,选择LCD Interface LCD Register Select: 根据选择计算映射地址, FSNC_A[25] Data: 数据宽度 NOR/PSRAM ti…