Rust 如何将具体类型与具体类型的选项进行比较?该类型是否未实现复制?
在这种情况下,使用移动值时出错Rust 如何将具体类型与具体类型的选项进行比较?该类型是否未实现复制?,rust,Rust,在这种情况下,使用移动值时出错 #[derive(PartialEq)] struct Something { name: String, } fn example() { fn get_something_maybe() -> Option<Something> { todo!() } fn do_with_something(thing: Something) { todo!() } le
#[derive(PartialEq)]
struct Something {
name: String,
}
fn example() {
fn get_something_maybe() -> Option<Something> {
todo!()
}
fn do_with_something(thing: Something) {
todo!()
}
let maybe_something = get_something_maybe();
let concrete_something = Something {
name: "blah".to_string(),
};
// how can I compare a concrete something to a maybe of something easily?
if Some(concrete_something) != maybe_something {
// I just want to compare the concrete thing to an option of the thing which are themselves comparable
do_with_something(concrete_something);
}
}
您可以与选项(而不是选项)进行比较:
您可以与选项(而不是选项)进行比较:
#[derive(PartialEq, Debug)]
struct Something {
name: String,
}
fn get_something_maybe() -> Option<Something> {
Some(Something {
name: "asdf".to_string(),
})
}
fn main() {
let maybe_something = get_something_maybe();
let concrete_something = Something {
name: "asdf".to_string(),
};
if Some(&concrete_something) == maybe_something.as_ref() {
println!("they're equal");
}
println!(
"neither has been moved: {:?} {:?}",
maybe_something, concrete_something
);
}