Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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());
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading