官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 伊宁县| 建水县| 正安县| 芜湖县| 辉县市| 浠水县| 连平县| 柘荣县| 长丰县| 奉贤区| 黄浦区| 金乡县| 宜春市| 汝阳县| 通山县| 武乡县| 福州市| 栾城县| 仲巴县| 梧州市| 垣曲县| 绥德县| 韶山市| 彰化市| 嘉黎县| 定襄县| 邵阳市| 尼玛县| 两当县| 临江市| 安仁县| 固始县| 康马县| 康乐县| 乐都县| 交城县| 宝坻区| 唐山市| 育儿| 恭城| 台东市|