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

Going wrong

Other than classes, Rust comes without another well-known companion: null. In the absence of pointers and with a very different memory management model, there is no typical null pointer/reference.

Instead, the language works with Option and Result types that let developers model success or failure. In fact, there is no exception system either, so any failed execution of a function should be indicated in the return type. Only in rare cases when immediate termination is required does the language provide a macro for panicking: panic!().

Option<T> and Result<T, E> both encapsulate one (Option<T>) or two (Result<T, E>) values that can be returned to communicate an error or whether something was found or not. For example, a find() function could return Option<T>, whereas something like read_file() would typically have a Result<T, E> return type to communicate the content or errors:

fn find(needle: u16, haystack: Vec<u16>) -> Option<usize> {
// find the needle in the haystack
}

fn read_file(path: &str) -> Result<String, io::Error> {
// open the path as a file and read it
}

Handling those return values is often done with match or if let clauses in order to handle the cases of success or failure:

match find(2, vec![1,3,4,5]) {
Some(_) => println!("Found!"),
None => println!("Not found :(")
}

// another way
if let Some(result) = find(2, vec![1,2,3,4]) {
println!("Found!")
}

// similarly for results!
match read_file("/tmp/not/a/file") {
Ok(content) => println!(content),
Err(error) => println!("Oh no!")
}

This is due to Option<T> and Result<T, E> both being enumerations that have generic type parameters; they can assume any type in their variants. Matching on their variants provides access to their inner values and types to allow a branch of the code to be executed and handle the case accordingly. Not only does this eliminate the need for constructs such as try/catch with multiple—sometimes cast—exception arms, it makes failure part of the normal workflow that needs to be taken care of.

主站蜘蛛池模板: 吉水县| 肇东市| 永康市| 库伦旗| 武宣县| 安泽县| 柘荣县| 冷水江市| 奉贤区| 广丰县| 思南县| 五莲县| 海城市| 筠连县| 信宜市| 新泰市| 五寨县| 岫岩| 浠水县| 米林县| 慈溪市| 陆川县| 阿瓦提县| 安岳县| 宾阳县| 平陆县| 翁牛特旗| 荣成市| 缙云县| 黑河市| 奇台县| 鹤庆县| 龙口市| 麦盖提县| 宜州市| 新余市| 神池县| 黎城县| 牟定县| 定兴县| 阿拉尔市|