extern crate postgres;
use postgres::{Connection, SslMode};
struct User {
reference: String,
email: String,
firstname: String,
lastname: String
}
static DB_URI: &'static str = "postgres://postgres:[email protected]/test";
fn main() {
let conn = Connection::connect(DB_URI, &SslMode::None).unwrap();
let trans = conn.transaction().unwrap();
//...
}
fn insert_user<'a>(trans: &'a postgres::Transaction, user: &User) -> &'a postgres::Result {
//...
}
數/類型是拋出一個錯誤
error: wrong number of type arguments: expected 1, found 0 [E0243]
fn insert_user<'a>(trans: &'a postgres::Transaction, user: &User) -> &'a postgres::Result {
^~~~~~~~~~~~~~~~
這到底是怎麼失蹤了?我只想返回執行查詢的結果。
UPDATE所以我修改了功能線是這樣的:
fn insert_user(trans: &postgres::Transaction, user: &User) -> &postgres::Result<()> {
編譯器誘騙正確的返回類型,它給了我:
mismatched types:
expected `core::result::Result<(), postgres::error::Error>`,
found `core::result::Result<postgres::Rows<'_>, postgres::error::Error>`
然而,當我試圖以匹配返回類型,如下所示:
fn insert_user(trans: &postgres::Transaction, user: &User) -> &postgres::Result<postgres::Rows<'_>, postgres::error::Error> {
它現在拋出一個新的錯誤:
error: use of undeclared lifetime name `'_` [E0261]
fn insert_user(trans: &postgres::Transaction, user: &User) -> postgres::Result<postgres::Rows<'_>, postgres::error::Error> {
^~
這正是它說。你給了它錯誤的類型參數。 ['Result'](http://sfackler.github.io/rust-postgres/doc/v0.9.1/postgres/type.Result.html)採用返回的有效值的類型。 – Veedrac