首页 > 其他分享 >[Rust] parse().unwrap()

[Rust] parse().unwrap()

时间:2024-02-19 21:22:20浏览次数:24  
标签:string unwrap number parse let cases Rust

This lesson explains Type Inference in Rust and how it allows the compiler to figure out by itself, what type variables have when they get their values assigned.

There are cases when the compiler cannot infer a value's type through static analysis. In such cases, developers have to provide type annotations explicitly.

 

In many cases, Rust can infer the types from the code:

#[allow(warnings)]

fn main() {

  let condition = false;

  let some_number = 5;

  let string_number = "5";

  let names = ["Bob", "Alice", "Cindy"];
}

 

But some cases not:

let string_number = "5";
let number = string_number.parse().unwrap();

This is because parse().unwrap()can be a string or number. So you need to tell the compiler what is the expected output.

let number: String = string_number.parse().unwrap(); // to string
let number: int32 = string_number.parse().unwrap(); // to int32

 

Another way is to use turbofish syntax

let number = string_number.parse::<String>().unwrap();
let number = string_number.parse::<int32>().unwrap();

 

标签:string,unwrap,number,parse,let,cases,Rust
From: https://www.cnblogs.com/Answer1215/p/18021996

相关文章

  • [Rust] Use Impl with Struct
    useanyhow::{Result,anyhow};usestd::str::FromStr;fnget_input()->&'staticstr{return"0,9->5,98,0->0,89,4->3,42,2->2,17,0->7,46,4->2,00,9->2,93,4->1,40,0->8,85,5->8,2&q......
  • 远程控制软件RustDesk自建服务器全平台部署及使用教程
    RustDesk挺出名的一款远程控制,远程协助的开源软件。完美替代TeamViewer,ToDesk,向日葵等平台。关键支持自建服务器,更安全私密远程控制电脑!其中客户端支持安卓,且支持控制安卓手机。官方地址官网:https://rustdesk.com/开源地址:https://github.com/rustdesk/一、准备工作1,有自己......
  • 教你用Rust实现Smpp协议
    本文分享自华为云社区《华为云短信服务教你用Rust实现Smpp协议》,作者:张俭。协议概述SMPP(ShortMessagePeer-to-Peer)协议起源于90年代,最初由Aldiscon公司开发,后来由SMPP开发者论坛维护和推广。SMPP常用于在SMSC(ShortMessageServiceCenter,短信中心)和短信应用之间传输短消息......
  • [Rust] Macros vs. Functions
    InRust,theexclamationmark(!)afteranameindicatesthatitisamacroratherthanafunction.MacrosandfunctionsinRustarecalleddifferentlyandservedifferentpurposes.Macros,likeanyhow!,candothingsthatfunctionscannot,suchasgenera......
  • [Rust] Error handling with Enum
    Wecanuse ReusltenumtodoerrorhandlingtypeResult<V,E>{Err(E),Ok(V)} Example://():empty//uszie:justreturnaintegreaserrorfordemofnerror_me(throw:bool)->Result<(),usize>{ifthrow{returnEr......
  • Rust程序设计 第2版 电子书 pdf
    关注公众号:红宸笑。回复:电子书即可   本书是Rust领域经典参考书,由业内资深系统程序员编写,广受读者好评。书中全面介绍了Rust这种新型系统编程语言——具有无与伦比的安全性,兼具C和C++的高性能,并大大简化了并发程序的编写。第2版对上一版内容进行了重组和完善,新增了对......
  • [Rust] Intro Enum
    GoEnumpackagemaintypeGoEnum=intconst(FooGoEnum=iotaBarBaz)funcmain(){} TypescriptEnumenumTsEnum{Foo,Bar,Baz} RustEnumenumRsEnum{Foo,Bar,Baz}Whygooverenums...Theyaresimple......
  • extism 基于rust 开发的强大webassembly 框架
    extism基于rust开发的强大webassembly框架包含的特性使用简单 可以方便的开发基于webassembly的插件系统安全方便运行 包含了灵活的架构可以可以方便与多种语言进行通信(基本覆盖了主流的编程语言)说明目前基于webassembly的语言集成热度是越来越高了,webassembly很值......
  • Sparse Table Advanced Skills
    Storeatupleof(valueofmaximum,indexofmaximum,valueofthesecondmaximum).Tomergetwosegments,wecompareiftheindicesofthemaximumsarethesame.Theycanpossiblybethesamebecauseweoftenqueryintersectingsegmentsinthesparsetab......
  • 48从零开始用Rust编写nginx,搭建一个简单又好看官方网站
    wmproxywmproxy已用Rust实现http/https代理,socks5代理,反向代理,负载均衡,静态文件服务器,websocket代理,四层TCP/UDP转发,内网穿透等,会将实现过程分享出来,感兴趣的可以一起造个轮子项目地址国内:https://gitee.com/tickbh/wmproxygithub:https://github.com/tickbh/wmpro......