Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 隐式和显式案例分析的主要区别是什么?_Rust - Fatal编程技术网

Rust 隐式和显式案例分析的主要区别是什么?

Rust 隐式和显式案例分析的主要区别是什么?,rust,Rust,在阅读文章时,我想知道经常提到的“明确案例分析”的反面是什么。我知道并理解此代码示例使用显式案例分析: fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { CliError::Io(ref err) => write!(f, "IO error: {}", err), CliError::Parse(ref err) => write!(

在阅读文章时,我想知道经常提到的“明确案例分析”的反面是什么。我知道并理解此代码示例使用显式案例分析:

fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match *self {
        CliError::Io(ref err) => write!(f, "IO error: {}", err),
        CliError::Parse(ref err) => write!(f, "Parse error: {}", err),
    }
}

但是什么是隐式案例分析?

在本书中,案例分析意味着通过直接分析
枚举
的每个案例来执行计算,例如使用
匹配
if let
表达式。书中给出的一个极端例子是

fn file_path_ext_explicit(file_path: &str) -> Option<&str> {
    match file_name(file_path) {    // <-- case analysis
        None => None,
        Some(name) => match extension(name) {   // <-- another case analysis
            None => None,
            Some(ext) => Some(ext),
        }
    }
}
然后可以将
file\u path\u ext\u explicit
函数简化为

fn file_path_ext(file_path: &str) -> Option<&str> {
    // no `match` expressions
    file_name(file_path).and_then(extension)
}
fn文件路径外部(文件路径:&str)->选项{
//没有“匹配”的表达
文件名(文件路径)。然后(扩展名)
}

在本书中,案例分析指通过直接分析
枚举
的每个案例来执行计算,例如使用
匹配
if let
表达式。书中给出的一个极端例子是

fn file_path_ext_explicit(file_path: &str) -> Option<&str> {
    match file_name(file_path) {    // <-- case analysis
        None => None,
        Some(name) => match extension(name) {   // <-- another case analysis
            None => None,
            Some(ext) => Some(ext),
        }
    }
}
然后可以将
file\u path\u ext\u explicit
函数简化为

fn file_path_ext(file_path: &str) -> Option<&str> {
    // no `match` expressions
    file_name(file_path).and_then(extension)
}
fn文件路径外部(文件路径:&str)->选项{
//没有“匹配”的表达
文件名(文件路径)。然后(扩展名)
}
它更清楚地表达了函数的意图,并且不容易出现逻辑错误