告诉我错误是什么或者从哪里寻找原因?为什么在这种情况下我们会得到不同的类型?
let client = Client::connect("postgresql://user:pass@localhost:5432/base", NoTls).unwrap();
match client {
Ok(mut client) => {
let stmt = client.prepare("INSERT INTO status (status) VALUES ($1)").unwrap();
client
.execute(&stmt, &[&json_data])
.expect("Failed to execute statement");
println!("JSON data inserted successfully");
}
Err(err) => println!("Error: {}", err),
}
给出消息:
error[E0308]: mismatched types
--> src/main.rs:252:9
|
251 | match client {
| ------ this expression has type `postgres::Client`
252 | Ok(mut client) => {
| ^^^^^^^^^^^^^^ expected `Client`, found `Result<_, _>`
|
= note: expected struct `postgres::Client`
found enum `Result<_, _>`
unwrap()
将物体拉出Result
,也就是说postgres::Client
,您无法再检查它是否OK
和Err
,因为Result
。如果要检查
Result
或OK
,Err
请将其移除.unwrap()
,然后冷静检查(同时client
它会在检查过程中自行离开那里)。Client::connect 表达式返回 Result<postgres::Client, postgres::Error>,也就是说,它是结果,而不是 Client 对象本身。从代码来看,您似乎正试图在 match 块中直接将其用作客户端