How to properly import structs in Rust?

I wanted to create a struct named "Filme" and include it in the, but I’ve been struggling with that. Here is the 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,
} file:

pub mod Filme;

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

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());


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