- 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.
- INSTANT Mock Testing with PowerMock
- WildFly:New Features
- GitLab Cookbook
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- 認識編程:以Python語言講透編程的本質
- Programming ArcGIS 10.1 with Python Cookbook
- Responsive Web Design with HTML5 and CSS3
- ANSYS Fluent 二次開發指南
- C語言程序設計
- JavaScript程序設計(第2版)
- QPanda量子計算編程
- Vue.js光速入門及企業項目開發實戰
- Python 3.6從入門到精通(視頻教學版)
- Building Microservices with .NET Core 2.0(Second Edition)
- 威脅建模:設計和交付更安全的軟件