use of undeclared crate or module, "use crate_name::schema::posts" doesn't always work

12,641

The difference between example.rs and post.rs is that post.rs is in the library crate web_project_core while example.rs is in the binary crate main. The path web_project_core::schema::posts is not available in post.rs as web_project_core is the current crate, rather than a dependency. Instead of web_project_core::schema::posts you could use crate::schema::posts, when referring to the current crate. The binary implicitly depends on the library as they are in the same package, making the path web_project_core::schema::posts available in example.rs

Share:
12,641

Related videos on Youtube

commonUser
Author by

commonUser

Updated on June 04, 2022

Comments

  • commonUser
    commonUser almost 2 years

    I'm trying to learn Rust by using it with actix-web and diesel.

    When I try to import/use the schema by using the crate name it only works in the example.rs file but not in the post.rs file. Both files are nested in their own folders and the command I'm using is the following:

    use web_project_core::schema::posts;

    When I use this other command instead, it works in post.rs but not in example.rs:

    use super::super::schema::posts;

    What am I missing?

    // Cargo.toml
    [lib]
    name = "web_project_core"
    path = "src/lib.rs"
    
    [[bin]]
    name = "main"
    path = "src/main.rs"
    
    // main.rs
    
    use actix_web::{App, HttpServer};
    
    mod handlers;
    
    // lib.rs
    #[macro_use]
    extern crate diesel;
    extern crate dotenv;
    
    use diesel::prelude::*;
    use diesel::pg::PgConnection;
    use dotenv::dotenv;
    use std::env;
    
    pub mod schema;
    pub mod modelz;
    
    // post.rs
    
    use serde::{Serialize, Deserialize};
    use nanoid::generate;
    
    use super::super::schema::posts;        // <-- it works
    // use web_project_core::schema::posts; // <-- it doesn't work
    
    // example.rs
    
    use actix_web::{get, web, post, HttpResponse, Responder};
    use diesel::prelude::*;
    
    use web_project_core::establish_connection;
    use web_project_core::schema::posts;            // <-- it works
    // use super::super::schema::posts;             // <-- it doesn't work
    use web_project_core::modelz::post::*;
    

    The project structure:

    Project structure

    Thanks