How to properly import structs in Rust?

I wanted to create a struct named "Filme" and include it in the main.rs, but I’ve been struggling with that. Here is the Filme.rs file:

pub struct Filme{
    pub nome: String,
     pub duracao: u8,
     pub categorias : String,
 
 }
 
 impl Filme{
     pub fn new(nome:String, duracao:u8, categorias: String) -> Filme{
         Filme {
             nome: nome,
             duracao: duracao,
             categorias: categorias,
         }
     }
}

main.rs file:

pub mod Filme;

fn main() {
    let mut filme = Filme::new("Vingadores".to_string(),90,"Ação e Aventura".to_string());
    println!("{}",filme.print_categorias());
}

By the way, both of them are in the same directory.

I keep getting the error "error[E0425]: cannot find function new in module Filme"

>Solution :

Don’t name the mod the same as your struct. You’d have to do Filme::Filme each time you reference the struct, or import it as as different alias. Just rename the module to something like filme.

But if you must,

mod Filme;

fn main() {
    let mut filme = Filme::Filme::new("Vingadores".to_string(), 90, "Ação e Aventura".to_string());
}

Or

use Filme::Filme as Film;

fn main() {
    let mut filme = Film::new("Vingadores".to_string(), 90, "Ação e Aventura".to_string());
}

Leave a Reply