我遇到了这个错误的大问题,我不明白它到底想要什么。该错误表示:由于不满足特征边界,无法在 &option<person>
&Option<Person>
到底是什么意思?我忽略了什么?
代码示例:人.rs
#[derive(Clone)]
pub struct Person {
pub firstname: String,
pub lastname: String,
pub age: u8
}
impl Person {
pub fn new(firstname: String, lastname: String, age: u8) -> Person {
Person{firstname, lastname, age}
}
}
lib.rs
use std::sync::Mutex;
mod person;
static CUSTOMER: Mutex<Option<person::Person>> = Mutex::new(None);
#[tauri::command]
fn add_customer() -> () {
*CUSTOMER.lock().unwrap() = Some(person::Person {
firstname: String::from("Firstname"),
lastname: String::from("Lastname"),
age: 72
});
}
#[tauri::command] // Error: method cannot be called on `&Option<Person>` due to unsatisfied trait bounds
fn get_customer() -> Option<person::Person> {
return CUSTOMER.lock().unwrap().clone();
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![
add_customer
get_customer,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
错误:
error[E0308]: mismatched types
--> src\lib.rs:19:12
|
18 | fn get_customer() -> Option<person::Person> {
| ---------------------- expected `Option<Person>` because of return type
19 | return CUSTOMER.lock().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<Person>`, found `MutexGuard<'_, Option<Person>>`
|
= note: expected enum `Option<_>`
found struct `MutexGuard<'_, Option<_>, >`
error[E0599]: the method `blocking_kind` exists for reference `&Option<Person>`, but its trait bounds were not satisfied
--> src\lib.rs:17:1
|
17 | #[tauri::command]
| ^^^^^^^^^^^^^^^^^ method cannot be called on `&Option<Person>` due to unsatisfied trait bounds
...
28 | .invoke_handler(tauri::generate_handler![
| _________________________-
29 | | add_customer,
30 | | get_customer
31 | | ])
| |_________- in this macro invocation
|
::: C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\option.rs:571:1
|
571 | pub enum Option<T> {
| ------------------ doesn't satisfy `Option<Person>: IpcResponse`
|
= note: the following trait bounds were not satisfied:
`Option<Person>: IpcResponse`
which is required by `&Option<Person>: tauri::ipc::private::ResponseKind`
= note: this error originates in the macro `__cmd__get_customer` which comes from the expansion of the macro `tauri::generate_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `operation-robot` (lib) due to 2 previous errors
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct Person {
pub firstname: String,
pub lastname: String,
pub age: u8
}
成功了,非常感谢!献给那些和我一样也在奋斗的人。