- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 153字
- 2021-07-02 23:07:23
The use of expect
Rust contains a very useful function called expect. This method is used with any form of call that has an Option or a Result type (for example, Result in the file-writing example has the options of File or Error). It works by moving the value out of the option and returning it. If the option/result type contains an error, the expect call stops your program and prints out the error message.
For example, the following statement will return either File or Error into file:
let file: Result<File, Error> = options.open("my_file.txt").expect("Opening the file failed");
A shorter form is available in the unwrap method. This is the same as the expect method, but it doesn't print out anything in case of a failure. In general, Some(a).unwrap() will return a.
Expect is usually favored instead of unwrap, since the full error message makes it easier to find where the error came from.
- .NET之美:.NET關(guān)鍵技術(shù)深入解析
- Beginning C++ Game Programming
- Learning RabbitMQ
- jQuery EasyUI網(wǎng)站開發(fā)實戰(zhàn)
- 基于差分進化的優(yōu)化方法及應(yīng)用
- 21天學通C++(第6版)
- Flux Architecture
- Hack與HHVM權(quán)威指南
- 3D Printing Designs:Octopus Pencil Holder
- C Primer Plus(第6版)中文版【最新修訂版】
- 關(guān)系數(shù)據(jù)庫與SQL Server 2012(第3版)
- Learning Alfresco Web Scripts
- 從零開始學UI設(shè)計·基礎(chǔ)篇
- BackTrack 5 Cookbook
- 給產(chǎn)品經(jīng)理講技術(shù)