首页 > 其他分享 >[Rust] Handle errors in Rust using expect()

[Rust] Handle errors in Rust using expect()

时间:2024-02-23 14:33:53浏览次数:23  
标签:use errors let expect using Rust first

This lesson discusses how to improve error handling by configuring custom error messages using the expect() function.

 
use std::io;

fn main() {
    let mut first = String::new();
    io::stdin().read_line(&mut first);
    
    // Not recommned to use uwnwrap()
    let a:u32 = first.trim().parse().unwrap()
    
    // Recommend to use expect() for better error handling
    let a:u32 = first.trim().parse().expect("This is a not a valid number")
}

 

标签:use,errors,let,expect,using,Rust,first
From: https://www.cnblogs.com/Answer1215/p/18029454

相关文章

  • [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];......
  • Go 100 mistakes - #32: Ignoring the impact of using pointer elements in range lo
    Thissectionlooksataspecificmistakewhenusingarangeloopwithpointerelements.Ifwe’renotcautiousenough,itcanleadustoanissuewherewereferencethe wrongelements.Let’sexaminethisproblemandhowtofixit.Beforewebegin,let’scla......
  • rust结构体包含另一个结构体引用时,serde序列化问题
    代码如下useserde::{Deserialize,Serialize};#[derive(Serialize,Deserialize)]structPerson{id:String,name:String,}#[derive(Serialize,Deserialize)]structMsg<'a>{id:String,person:&'aPerson,}fnmain(){......
  • uniapp编译成微信小程序报错-Component is not found in path "components/canvaspage
     问题:我需要将components/canvaspagebg/index引入进pages/index/index   报错了pages/index/index页面引入: uni-app程序编译成微信小程序后,组件无法显示,控制台报错,错误信息为: 我查看了路径,是对的看网上的解决办法就是 我取消勾选后刷新页面就可以了,此时我在选中......