0

Today when I am build the rust project, tell me this error:

  --> src/biz/music/playlist.rs:22:5
   |
22 | use std::io;
   |     ^^^^^^^

error[E0308]: mismatched types
  --> src/biz/music/playlist.rs:38:20
   |
38 |           creator: user,
   |                    ^^^^ expected struct `playlist_response::user_response::UserResponse`, found struct `playlist::user_response::UserResponse`

error: aborting due to previous error; 5 warnings emitted

I only define one UserResponse struct, why still conflict? what should I do to avoid this problem? this is the code I am using now:

#[path = "../../config/db/config.rs"]
mod config;
#[path = "../../schema.rs"]
mod schema;
#[path = "../../models.rs"]
mod models;
#[path = "../../model/request/music/play_record_request.rs"]
mod play_record_request;
#[path = "../../model/response/api_response.rs"]
mod api_response;
#[path = "../../model/response/playlist_response.rs"]
mod playlist_response;

#[path = "../../model/response/user/user_response.rs"]
mod user_response;

use crate::diesel::prelude::*;
use models::{ QueryPlaylist };
use api_response::ApiResponse;
use playlist_response::PlaylistResponse;
use rocket::response::content;
use std::io;
use user_response::UserResponse;

#[get("/v1/playlist")]
pub fn user_playlist() -> content::Json<String> {
    use schema::playlist::dsl::*;
    let connection = config::establish_connection();
    let results = playlist.filter(creator.eq(1))
        .limit(1)
        .load::<QueryPlaylist>(&connection)
        .expect("Error loading posts");
    let mut reslist = Vec::new();
    let user = UserResponse{ id: 0, name: "test".to_string() };
    for re in &results {
      let playlist_dto = PlaylistResponse{
          id: re.id,
          creator: user,
          name: "test".to_string(),
          coverUrl: re.cover_url,
          description: re.description,
          subscribed: re.subscribed,
          subscribedCount: re.subscribed_count,
          commentCount: re.comment_count,
          shareCount: re.share_count,
          playCount: re.play_count
      };
       reslist.push(playlist_dto);
    }
    let res = ApiResponse {
        result: reslist,
        ..Default::default()
    };
    let response_json = serde_json::to_string(&res).unwrap();
    return content::Json(response_json);
}
Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • 1
    Does this answer your question? [How to include a module from another file from the same project?](https://stackoverflow.com/questions/26388861/how-to-include-a-module-from-another-file-from-the-same-project) – Elias Holzmann Sep 09 '21 at 11:41
  • 1
    You don't use `mod` to bring stuff from different files in scope, you need to use `use`. By using `mod`, you include `user_response.rs` twice, in effect having two different `UserResponse` structs. – Elias Holzmann Sep 09 '21 at 11:43
  • 1
    There are a lot of other questions about using sibling modules / parent modules / etc. If you still struggle to get it working after reading the answers to the linked questions, please [edit] the question to contain a [mre]. I will reopen it if it is demonstrably not a duplicate. – trent Sep 09 '21 at 12:34
  • Here's another explanation: [Why do I get "expected struct file1::A found struct file2::A" error when using a struct in multiple files?](https://stackoverflow.com/questions/68928911/why-do-i-get-expected-struct-file1a-found-struct-file2a-error-when-using-a) – kmdreko Sep 09 '21 at 15:15

0 Answers0