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

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.

主站蜘蛛池模板: 唐河县| 新昌县| 汕头市| 连江县| 吴堡县| 鸡泽县| 伊春市| 明水县| 芦溪县| 汉寿县| 厦门市| 宾阳县| 江陵县| 井冈山市| 祁门县| 嫩江县| 奎屯市| 九寨沟县| 漳平市| 安新县| 绿春县| 洪雅县| 永嘉县| 宾川县| 定襄县| 化州市| 南丹县| 多伦县| 兴宁市| 台东县| 宜良县| 道真| 建瓯市| 中江县| 和静县| 棋牌| 卫辉市| 泸西县| 玉屏| 绥中县| 张家口市|