skip to Main Content

I am learning rust, now tried to import a struct, this is the source file path app_root/src/model/response/playlist_response.rs, I am import another struct like this:

#[path = "../../models.rs"]
mod models;

use models::QueryPlaylist;

the model.rs path is app_root/src/models.rs, the models.rs file look like this:

use super::schema::posts;
use super::schema::playing_record;
use super::schema::favorites;
use super::schema::songs;
use super::schema::playlist;
use super::schema::album;
use rocket::serde::Serialize;

#[derive(Insertable,Serialize,Queryable)]
#[table_name="playlist"]
pub struct QueryPlaylist {
    pub id: i64,
    pub creator: i64,
    pub name: String,
    pub cover_url: Option<String>,
    pub description: Option<String>,
    pub subscribed: Option<i32>,
    pub subscribed_count: Option<i64>,
    pub comment_count: Option<i64>,
    pub share_count: Option<i32>,
    pub play_count: Option<i32>
}

when I compile the project, shows error like this:

$ cargo build                                                                                                                                        ‹ruby-2.7.2›
   Compiling reddwarf_music v0.1.0 (/Users/dolphin/Documents/GitHub/reddwarf_music)
error[E0432]: unresolved import `super::schema`
 --> src/biz/music/../../model/response/../../models.rs:1:12
  |
1 | use super::schema::posts;
  |            ^^^^^^ could not find `schema` in `super`

error[E0432]: unresolved import `super::schema`
 --> src/biz/music/../../model/response/../../models.rs:2:12
  |
2 | use super::schema::playing_record;
  |            ^^^^^^ could not find `schema` in `super`

error[E0432]: unresolved import `super::schema`
 --> src/biz/music/../../model/response/../../models.rs:3:12
  |
3 | use super::schema::favorites;
  |            ^^^^^^ could not find `schema` in `super`

error[E0432]: unresolved import `super::schema`
 --> src/biz/music/../../model/response/../../models.rs:4:12
  |
4 | use super::schema::songs;
  |            ^^^^^^ could not find `schema` in `super`

error[E0432]: unresolved import `super::schema`
 --> src/biz/music/../../model/response/../../models.rs:5:12
  |
5 | use super::schema::playlist;
  |            ^^^^^^ could not find `schema` in `super`

error[E0432]: unresolved import `super::schema`
 --> src/biz/music/../../model/response/../../models.rs:6:12
  |
6 | use super::schema::album;
  |            ^^^^^^ could not find `schema` in `super`

the schema.rs file path is app_root/src/schema.rs. I am import using the wrong way? what is the problem like this way? what should I do to make it work? This is the main.rs:

#[macro_use]
extern crate rocket;

#[macro_use]
extern crate diesel;

// #[macro_use]
// extern crate redis;

#[path="biz/music/music.rs"] mod music;
#[path="config/db/config.rs"] mod config;
#[path="biz/music/songs.rs"] mod songs;
#[path="biz/music/test.rs"] mod test;
#[path="biz/user/user.rs"] mod user;
#[path="biz/music/playlist.rs"] mod playlist;


#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/music/music", routes![music::play_record,music::save_play_record,music::like,music::dislike])
        .mount("/music/songs",routes![songs::detail])
        .mount("/music/user",routes![user::recent_found])
        .mount("/music/playlist",routes![playlist::user_playlist])
        .mount("/music/test", routes![test::detail])
}

2

Answers


  1. You should not use #[path = ...] to try to import models.rs. It will declare a new module instead of using it as declared in main.rs. This in turn messes up how it imports stuff since super doesn’t mean the same thing.

    Instead you should simply import it using:

    use crate::models;
    

    See also:

    Login or Signup to reply.
  2. Generally this is how you work with different files in your project:
    Say you have this structure:

    .
    ├── app
    │   ├── some_logic.rs
    ├── database
    │   ├── models.rs
    ├── config.rs
    └── main.rs
    

    In main.rs you declare:

    mod config;
    mod database{
        pub mod models;
    }
    mod app {
        pub mod some_logic;
    }
    

    Then you can use any public member of i.e. models.rs with: database::models::SomeStruct.
    Same goes for some_logic.rs whose members you access with app::some_logic::SomeStruct. Of course config.rs‘s members are simply accessed with config::SomeStruct.

    Say you want to access a constant MY_CONSTANT withing main.rs from models.rs:

    fn function_within_models_rs() {
        println!("Constant: {}", crate::MY_CONSTANT)
    }
    

    And accessing pub const LOGIC_CONSTANT: i32 = 222; declared in some_logic.rs from within models.rs:

    fn function_within_models_rs() {
        println!("Constant: {}", crate::app::some_logic::LOGIC_CONSTANT)
    }
    

    As you see, elements that are accessed from outside of the module there are declared in, need to be declared as pub (public). The compiler is great in giving you hints about what needs to be set to public so I would suggest keeping things private until you really want to use them somewhere else.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search