看了 Rust 的基础视频,然后又跟着码 web 相关的视频学习;
学习的视频链接,https://www.bilibili.com/video/BV1RP4y1G7KF
其中,第 16 节课的时候,感觉是按着教学码出来的,但是一直 cargo check 报错,也不知道怎么改了。
报错的提示:
error[E0308]: mismatched types
--> webservice/src/bin/../dbaccess/course.rs:7:30
|
7 | let rows: Vec<Course> = sqlx::query_as!(Course,
| ______________________________^
8 | | r#"select * from course where teacher_id = $1"#,
9 | | teacher_id
10 | | )
| |_____^ expected enum `std::option::Option`, found struct `std::string::String`
|
= note: expected enum `std::option::Option<std::string::String>`
found struct `std::string::String`
= note: this error originates in the macro `$crate::sqlx_macros::expand_query` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try wrapping the expression in `errors::_::_serde::__private::Some`
|
554| errors::_::_serde::__private::Some($crate::sqlx_macros::expand_query!(record = $out_struct, source = $query, args = [$($args)*]))
| +++++++++++++++++++++++++++++++++++ +
其中代码部分是:
dbaccess/course.rs
pub async fn get_courses_for_teacher_db(pool: &PgPool, teacher_id: i32,) -> Result<Vec<Course>, MyError> {
let rows: Vec<Course> = sqlx::query_as!(Course,
r#"select * from course where teacher_id = $1"#,
teacher_id
)
.fetch_all(pool)
.await?;
Ok(rows)
}
models/course.rs 中 Course 的类
#[derive(Serialize, Debug, Clone, sqlx::FromRow)]
pub struct Course {
pub teacher_id: i32,
pub id: i32,
pub name: String,
pub time: Option<NaiveDateTime>,
pub description: Option<String>,
pub format: Option<String>,
pub structure: Option<String>,
pub duration: Option<String>,
pub price: Option<i32>,
pub language: Option<String>,
pub level: Option<String>
}
不知道,是否有同学看过这个视频,跟着码过代码部分?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.