8

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

commonUser
  • 599
  • 1
  • 6
  • 17

1 Answers1

9

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

Skgland
  • 862
  • 10
  • 24