Cannot find value `t` in this scope

I am a new learner of rust programming.
I am trying with the tutorial from youtube , and I got an error when I run the cargo run.

Here is my code:
src/schema.rs

table! {
    books (id) {
        id -> Int4,
        title -> Varchar,
        author -> Varchar,
        published -> Bool,
    }
}

src/model.rs

use diesel;
use diesel::prelude::*;
use diesel::pg::PgConnection;

use schema::books;
use schema::books::dsl::books as all_books;

#[derive(Queryable)]
pub struct Book {
    pub id: i32,
    pub title: String,
    pub author: String,
    pub published: bool,
}

#[derive(Insertable)]
#[table_name = "books"]
pub struct NewBook {
    pub title: String,
    pub author: String,
    pub published: bool,
}

impl Book {
    pub fn show(id: i32, conn: &PgConnection) -> Vec<Book> {
        all_books
            .find(id)
            .load::<Book>(conn)
            .expect("Error loading book")
    }

    pub fn all(conn: &PgConnection) -> Vec<Book> {
        all_books
            .order(books::id.desc())
            .load::<Book>(conn)
            .expect("error loading the books")
    }

    pub fn update_by_id(id: i32, conn: &PgConnection, book: NewBook) -> bool {
        use schema::books::dsl::{author as a, published as p, title as t};
        let NewBook {
            title,
            author,
            published,
        } = book;

        diesel::update(all_books.find(id))
            .set((a.eq(author), p.eq(published), t.eq(title)))
            .get_result::<Book>(conn)
            .is_ok()
    }

    pub fn insert(book: NewBook, conn: &PgConnection) -> bool {
        diesel::insert_into(books::table)
            .values(&book)
            .execute(conn)
            .is_ok()
    }

    pub fn delete_by_id(id: i32, conn: &PgConnection) -> bool {
        if Book::show(id, conn).is_empty() {
            return false;
        };
        diesel::delete(all_books.find(id)).execute(conn).is_ok()
    }

    pub fn all_by_author(author: String, conn: &PgConnection) -> Vec<Book> {
        all_books
            .filter(books::author.eq(author))
            .load::<Book>(conn)
            .expect("Error loading books by author")
    }
}

src/main.rs

#[macro_use]
extern crate diesel;

extern crate dotenv;

use dotenv::dotenv;
use std::env;
use diesel::prelude::*;
use diesel::pg::PgConnection;

mod schema;
mod models;

fn main() {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL").expect("set DATABASE_URL");
    let conn = PgConnection::establish(&database_url).unwrap();

    let book = models::NewBook {
        title: String::from("Gravity's Rainbow"),
        author: String::from("Thomas Pynchon"),
        published: true,
    };

    if models::Book::insert(book, &conn) {
        println!("success");
    } else {
        println!("failed");
    }
}

Cargo.toml

[package]
name = "api"
name = "api"

version = "0.1.0"
authors = ["kanel soengkanel@gmail.com"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
diesel = {version="1.4.5", features=["postgres"] }
dotenv = "0.15.0"

Error Message

  Compiling api v0.1.0 (/Users/kanel/Documents/Developments/rustcode/api)
error[E0433]: failed to resolve: use of undeclared type or module `schema`
 --> src/models.rs:6:5
  |
6 | use schema::books::dsl::books as all_books;
  |     ^^^^^^ use of undeclared type or module `schema`

error[E0433]: failed to resolve: use of undeclared type or module `schema`
  --> src/models.rs:40:13
   |
40 |         use schema::books::dsl::{author as a, published as p, title as t};
   |             ^^^^^^ use of undeclared type or module `schema`

error[E0432]: unresolved import `schema`
 --> src/models.rs:5:5
  |
5 | use schema::books;
  |     ^^^^^^ help: a similar path exists: `crate::schema`
  |
  = note: `use` statements changed in Rust 2018; read more at <https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html>

error[E0425]: cannot find value `all_books` in this scope
  --> src/models.rs:26:9
   |
26 |         all_books
   |         ^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `all_books` in this scope
  --> src/models.rs:33:9
   |
33 |         all_books
   |         ^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `all_books` in this scope
  --> src/models.rs:47:24
   |
47 |         diesel::update(all_books.find(id))
   |                        ^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `a` in this scope
  --> src/models.rs:48:19
   |
48 |             .set((a.eq(author), p.eq(published), t.eq(title)))
   |                   ^ not found in this scope

error[E0425]: cannot find value `p` in this scope
  --> src/models.rs:48:33
   |
48 |             .set((a.eq(author), p.eq(published), t.eq(title)))
   |                                 ^ not found in this scope

error[E0425]: cannot find value `t` in this scope
  --> src/models.rs:48:50
   |
48 |             .set((a.eq(author), p.eq(published), t.eq(title)))
   |                                                  ^ not found in this scope

error[E0425]: cannot find value `all_books` in this scope
  --> src/models.rs:64:24
   |
64 |         diesel::delete(all_books.find(id)).execute(conn).is_ok()
   |                        ^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `all_books` in this scope
  --> src/models.rs:68:9
   |
68 |         all_books
   |         ^^^^^^^^^ not found in this scope

error: aborting due to 11 previous errors

Some errors have detailed explanations: E0425, E0432, E0433.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `api`.

To learn more, run the command again with --verbose.
kanel@kanelsoengs-MacBook-Pro api % 

Please community help to check.

This is the error you should focus on fixing:

 --> src/models.rs:6:5
  |
6 | use schema::books::dsl::books as all_books;
  |     ^^^^^^ use of undeclared type or module `schema`

I assume you have a schema.rs file. You have to tell Rust about it with a mod schema; statement. Additionally, you should import things from the local project with crate, i.e. use crate::your::path; instead of just using the module name directly. Using the module name is only possible in the same file as the mod statement, and it is less confusing to just use use crate:: everywhere.

can you help to provide more detail on how to fixing this issue?

Thanks,

It looks like the tutorial was made for the 2015 edition of Rust. Does your Cargo.toml look like in the video, or is there an extra edition = "2018" line in there? You can read about editions here.

Do you have a mod schema; anywhere in your project? And if you're on the 2015 edition, go change that to the 2018 edition.

yes, there is a line in my cargo.toml.

@alice, yes I have updated the question by included the file schema.rs as well.

You need crate:: in front of imports of stuff from your own project.

use crate::schema::books;
use crate::schema::books::dsl::books as all_books;

The crate keyword is only optional if the use statement is in the same file as the mod statement.

@alice, I added added as your instruction. I still get the following error message:

 cargo run
   Compiling api v0.1.0 (/Users/kanel/Documents/Developments/rustcode/api)
error[E0433]: failed to resolve: use of undeclared type or module `schema`
  --> src/models.rs:40:13
   |
40 |         use schema::books::dsl::{author as a, published as p, title as t};
   |             ^^^^^^ use of undeclared type or module `schema`

error[E0425]: cannot find value `a` in this scope
  --> src/models.rs:48:19
   |
48 |             .set((a.eq(author), p.eq(published), t.eq(title)))
   |                   ^ not found in this scope

error[E0425]: cannot find value `p` in this scope
  --> src/models.rs:48:33
   |
48 |             .set((a.eq(author), p.eq(published), t.eq(title)))
   |                                 ^ not found in this scope

error[E0425]: cannot find value `t` in this scope
  --> src/models.rs:48:50
   |
48 |             .set((a.eq(author), p.eq(published), t.eq(title)))
   |                                                  ^ not found in this scope

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0425, E0433.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `api`.

To learn more, run the command again with --verbose.
kanel@kanelsoengs-MacBook-Pro api %
  --> src/models.rs:40:13
   |
40 |         use schema::books::dsl::{author as a, published as p, title as t};
   |             ^^^^^^ use of undeclared type or module `schema`

You need crate:: on all imports of files from your own project.

1 Like

@alice, seem good works now, but I just got some warning ,maybe I should ignore this warning :zipper_mouth_face:

kanel@kanelsoengs-MacBook-Pro api % cargo run
   Compiling api v0.1.0 (/Users/kanel/Documents/Developments/rustcode/api)
warning: associated function is never used: `show`
  --> src/models.rs:25:12
   |
25 |     pub fn show(id: i32, conn: &PgConnection) -> Vec<Book> {
   |            ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: associated function is never used: `all`
  --> src/models.rs:32:12
   |
32 |     pub fn all(conn: &PgConnection) -> Vec<Book> {
   |            ^^^

warning: associated function is never used: `update_by_id`
  --> src/models.rs:39:12
   |
39 |     pub fn update_by_id(id: i32, conn: &PgConnection, book: NewBook) -> bool {
   |            ^^^^^^^^^^^^

warning: associated function is never used: `delete_by_id`
  --> src/models.rs:60:12
   |
60 |     pub fn delete_by_id(id: i32, conn: &PgConnection) -> bool {
   |            ^^^^^^^^^^^^

warning: associated function is never used: `all_by_author`
  --> src/models.rs:67:12
   |
67 |     pub fn all_by_author(author: String, conn: &PgConnection) -> Vec<Book> {
   |            ^^^^^^^^^^^^^

warning: 5 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.59s
     Running `target/debug/api`
success
kanel@kanelsoengs-MacBook-Pro api %

That warning will go away if you actually use the functions. If you don't intend to use it, you should just delete the function.

1 Like

Yes, @alice, thank you so much for your help.