首页 > 其他分享 >[Rust] Handle errors in Rust using Pattern Matching

[Rust] Handle errors in Rust using Pattern Matching

时间:2024-02-23 14:49:07浏览次数:20  
标签:mut errors Handle Pattern using Matching Rust first

In this lesson we'll explore how to unwrap a Result type using a language feature called Pattern Matching.

 
use std::io;

fn main() {
    let mut first = String::new();
    io::stdin().read_line(&mut first).unwrap();
    
    let mut a:u32 = 0;
    
    match first.trim().parse() {
        Ok(val) => {
            a = val;
        },
        Err(err) => {
            println!("This is not a valid number")
        }
    }
}

 

标签:mut,errors,Handle,Pattern,using,Matching,Rust,first
From: https://www.cnblogs.com/Answer1215/p/18029463

相关文章

  • [Rust] Exit a program using std::process in Rust
    Inthislessonwe'lllearnhowtoexitaprogramusingthe std::process moduleinRustandit's exit() method. usestd::io;usestd::process;fnmain(){letmutfirst=String::new();io::stdin().read_line(&mutfirst).unwrap()......
  • [Rust] Create a loop in Rust
    ThislessonshowshowtouseaRust loop torunaprograminfinitely. usestd::io;usestd::process;fnmain(){loop{println!("Pleaseenterafirstnumber:");leta=read_user_input();println!("Plea......
  • [Rust] Handle errors in Rust using expect()
    Thislessondiscusseshowtoimproveerrorhandlingbyconfiguringcustomerrormessagesusingthe expect() function. usestd::io;fnmain(){letmutfirst=String::new();io::stdin().read_line(&mutfirst);//Notrecommnedto......
  • [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......
  • dremio FileDatasetHandle 简单说明
    FileDatasetHandle是DatasetHandle的扩展,核心是对于文件系统类的处理(dremio对于文件系统的支持主要是基于hdfs)同时支持对于不同表格式的处理(deltalake,iceber,parquet),单独说明FileDatasetHandle主要是因为此实现比较重要dremio不少文件的处理都和此有关联FileDatasetHandle的一......
  • [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];......