首页 > 其他分享 >[Rust] Create a loop in Rust

[Rust] Create a loop in Rust

时间:2024-02-23 14:47:18浏览次数:26  
标签:read Create let Rust println input loop u32

This lesson shows how to use a Rust loop to run a program infinitely.

 

use std::io;
use std::process;

fn main() {
    loop {
        println!("Please enter a first number: ");
        let a = read_user_input();
    
        println!("Please enter a second number: ");
        let b = read_user_input();
    
        let result = sum(a, b);
        println!("{} + {} = {}", a, b, result);
    }
}

fn sum(a: u32, b: u32) -> u32 {
    a + b
}

fn read_user_input() -> u32 {
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
    
    let digit:u32;
    
    match input.trim().parse() {
        Ok(val) => digit = val,
        Err(_err) => {
            println!("Not a valid number!");
            process::exit(1);
        }
    };
    
    digit
}

 

标签:read,Create,let,Rust,println,input,loop,u32
From: https://www.cnblogs.com/Answer1215/p/18029475

相关文章

  • [Rust] Handle errors in Rust using expect()
    Thislessondiscusseshowtoimproveerrorhandlingbyconfiguringcustomerrormessagesusingthe expect() function. usestd::io;fnmain(){letmutfirst=String::new();io::stdin().read_line(&mutfirst);//Notrecommnedto......
  • CreateHolesInImage说明文档-对于遥感影像的空洞创建多边形矢量数据
    提取遥感影像的空洞地理处理工具箱特点:通用地理处理工具,支持任何遥感影像,包括无人机,卫星遥感,普通图片和gdb,mdb数据库等。速度快,极致效率,效率高,支持对多个文件夹下的任意多数据进行批处理使用简单,全自动话,无人工干预功能:提取空洞提取空洞和非空洞默认临时文件夹,结果文件夹默认临时......
  • 02. Create Project 创建项目导入素材
    使用版本我因为需要开发微信小游戏,所以使用的是2022.3.8f1c1版本创建项目首先创建一个3D项目,因为后续我们需要学习如何将一个普通的项目升级为URP通用渲染管线安装URP通用渲染管线在PackageManager中搜索Universal,然后安装UniversalRP。2022版本的Unity不需......
  • [Rust] Char vs String
    usestd::path::PathBuf;useclap::Parser;#[derive(Parser,Debug)]#[clap()]pubstructOpts{pubargs:Vec<String>,#[clap(short='c',long="config")]pubconfig:Option<PathBuf>,#[clap(short='p'......
  • Rust 编译报 spurious network error (1 tries remaining): [7] Couldn't connect to
    现象:当执行 cargobuild时报类似错误:warning:spuriousnetworkerror(3triesremaining):[7]Couldn'tconnecttoserver(Failedtoconnectto127.0.0.1port8899after2040ms:Couldn'tconnecttoserver);class=Net(12)warning:spuriousnetworkerror......
  • Docker 运行图形界面版 aTrust
    1、Docker、Docker-Compose安装https://www.cnblogs.com/a120608yby/p/9883175.htmlhttps://www.cnblogs.com/a120608yby/p/14582853.html2、服务Docker-Compose配置#catdocker-compose.ymlversion:'3'services:atrust:image:hagb/docker-atrustc......
  • Go 100 mistakes - #35: Using defer inside a loop
    Wewillimplementafunctionthatopensasetoffileswherethefilepathsare receivedviaachannel.Hence,wehavetoiterateoverthischannel,openthefiles,and handletheclosure.Here’sourfirstversion:Thereisasignificantproblemwithth......
  • [Rust] Reference Types in Rust
    LearnhowtocreatereferencesinRustusingtheborrow-operator & andwhentheyareuseful.Foramorethoroughexplanationofreferencesandtheircharacteristics,checkoutthisblogpost:https://blog.thoughtram.io/references-in-rust/letname:St......
  • [Rust] Arrays in Rust
    InthislessonwetakealookatArraysandthedifferentwaysofcreatingthem.ArraysinRustarecollectionsofvaluesofthesametypethatcannotchangeinsize.Inotherwords,thenumberoffields(orelements)hastobeknownatcompiletime.#[al......
  • [Rust] Vectors in Rust
    Inthislessonyou'lllearnabout Vec<T>,or Vectors.VectorsarelikeArrays,acollectionofvaluesofthesametype,butasopposedtoArrays,Vectorscanchangeinsize.#[allow(warnings)]fnmain(){letmutnumbers=vec![1,4];......