gpt4 book ai didi

rust - 返回 Option 时有些不需要?

转载 作者:行者123 更新时间:2023-11-29 07:57:39 29 4
gpt4 key购买 nike

我正在做 more combinators “Rust by Example”的一部分,并决定自己去看看使用 map 需要付出多少努力而不是 and_then .

在我的尝试中,我遇到了一些非常奇怪的事情(对我来说它甚至看起来像是一个编译器错误)。它看起来我只需要返回一个Food当返回类型应该是 Option(Food) 时键入

在我看来,cookable下面的函数应该可以减少到一行:

have_ingredients(food).map(|f| can_cook(f))

当然也可以是这样的:

have_ingredients(food).and_then(can_cook)

尽管我看不出这两个函数之间的根本区别,因为它们都返回一个 Option<U>。 .

这样做时我遇到了一个奇怪的编译器错误,所以我明确地将匹配分解如下 - 看起来编译器想要返回 Food即使返回类型是 Some(Food) .这是怎么回事???

//! stack.rs
#[derive(Debug)]
enum Food {
CordonBleu,
Steak,
Sushi,
}

#[derive(Debug)]
enum Day {
Monday,
Tuesday,
Wednesday,
}

/// we don't have the ingredients for sushi
fn have_ingredients(food: Food) -> Option<Food> {
match food {
Food::Sushi => None,
_ => Some(food),
}
}

/// can cook anything but cordon blue
fn can_cook(food: Food) -> Option<Food> {
match food {
Food::CordonBlue => None,
_ => Some(food),
}
}

/// can be done using map
fn cookable(food: Food) -> Option<Food> {
match have_ingredients(food).map(|f| can_cook(f)) {
// Some(food) => food, // Why is this correct???
Some(food) => Some(food), // **error: mismatched types:
None => None,
}
}

fn eat(food: Food, day: Day) {
match cookable(food) {
Some(food) => println!("Yay! On {:?} we eat {:?}", day, food),
None => println!("Oh no we didn't get to eat on {:?}!", day),
};
}

fn main() {
let (cordon_bleu, steak, sushi) = (Food::CordonBleu, Food::Steak, Food::Sushi);
eat(cordon_bleu, Day::Monday);
eat(steak, Day::Tuesday);
eat(sushi, Day::Wednesday);
}

这是上述程序的完整编译器错误:

 ch16_errors git:(master) ✗ rustc stack.rs
stack.rs:38:28: 38:32 error: mismatched types:
expected `Food`,
found `core::option::Option<Food>`
(expected enum `Food`,
found enum `core::option::Option`) [E0308]
stack.rs:38 Some(food) => Some(food),
^~~~
stack.rs:38:28: 38:32 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error

最佳答案

have_ingredients(food).map(|f| can_cook(f))

给出 Option<Option<Food>> ,不是 Option<Food> , 自 map不会压平值。

考虑

Option<T>::map(Fn(T) -> U)

这会转换 Option<T>Option<U> .从而让T = Food , U = Option<Food>can_cook给出实例化

Option<Food>::map(Fn(Food) -> Option<Food>)

给出一个 Option<Option<Food>> .

因此 Some(food)比赛中有food类型 Option<Food> .

and_then扁平化结果类型,这就是为什么它不会发生这种情况。

关于rust - 返回 Option 时有些不需要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34968848/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com