2016-06-03 7 views
3

Ich habe ein einfaches Projekt für Bildungszwecke, so habe ich eine Hauptfunktion und 3 Merkmale Battery, Display und GSM und Implementierungen für sie. Ich möchte, dass die die Hauptfunktion in der Datei main.rs und die drei Züge in einer anderen Datei namens phone.rs sein:Kann nicht verstehen, Rost-Modul-System

phone.rs

mod phone{ 
    pub struct Battery{ 
     model : String, 
     hours_idle : i16, 
     hours_talk : i16 
    } 

    pub struct Display{ 
     size : i16, 
     number_of_colors : i32 
    } 

    pub struct GSM{ 
     model : String, 
     manufactor : String, 
     price : f32, 
     owner : String, 
     battery: Battery, 
     display : Display 
    } 

    impl Battery { 
     pub fn new(model : String, hours_idle : i16, hours_talk : i16) -> Battery{ 
      Battery {model : model, hours_idle : hours_idle ,hours_talk : hours_talk} 
     } 
     pub fn print(&self){ 
      println!("Battery model: {}", self.model); 
      println!("Hours idle: {}", self.hours_idle); 
      println!("hours talk: {}", self.hours_talk); 
     } 
    } 

    impl Display { 
     pub fn new(size: i16, number_of_colors : i32) -> Display{ 
      Display{size : size, number_of_colors : number_of_colors} 
     } 
     pub fn print(&self){ 
      println!("size: {}", self.size); 
      println!("number_of_colors: {}", self.number_of_colors); 
     } 
    } 

    impl GSM { 
     pub fn new(model : String, manufactor : String, price : f32, owner : String, battery : Battery, display : Display) -> GSM{ 
      GSM{model : model, manufactor : manufactor, price : price, owner : owner, battery : battery, display : display } 
     } 
     pub fn print(&self){ 
      println!("Model: {}, Manufactor: {}", self.model, self.manufactor); 
      println!("price: {}", self.price); 
      println!("owner: {}", self.owner); 
      self.battery.print(); 
      self.display.print(); 
     } 
    } 
} 

main.rs

pub mod phone; 
fn main(){ 
    let battey = phone::Battery::new("modelBattery".to_string(), 1,2); 
    let display = phone::Display::new(10,20); 
    //model : String, manufactor : String, price : f32, owner : String, baterry : Battery, display : Display 
    let gsm = phone::GSM::new("modelPhone".to_string(), "manufactor".to_string(), 100.0, "owner".to_string(), battey, display); 
    gsm.print(); 
} 

Zuerst war alles in einer Datei (main.rs) und alles hat gut funktioniert (es gab einen Unterschied zum Beispiel habe ich phone:: vor Display,hinzugefügtund Battery in main.rs), wenn ich die Züge bewegt und hinzugefügt mod phone{} in phone.rs und pub mod phone in main.rs, bekam ich die folgenden Fehler vom Compiler:

src\main.rs:3:18: 3:37 error: failed to resolve. Could not find `Battery` in `phone` [E0433] 
src\main.rs:3  let battey = phone::Battery::new("modelBattery".to_string(), 1,2); 
           ^~~~~~~~~~~~~~~~~~~ 
src\main.rs:3:18: 3:37 help: run `rustc --explain E0433` to see a detailed explanation 
src\main.rs:3:18: 3:37 error: unresolved name `phone::Battery::new` [E0425] 
src\main.rs:3  let battey = phone::Battery::new("modelBattery".to_string(), 1,2); 
           ^~~~~~~~~~~~~~~~~~~ 
src\main.rs:3:18: 3:37 help: run `rustc --explain E0425` to see a detailed explanation 
src\main.rs:4:20: 4:39 error: failed to resolve. Could not find `Display` in `phone` [E0433] 
src\main.rs:4  let display = phone::Display::new(10,20); 
           ^~~~~~~~~~~~~~~~~~~ 
src\main.rs:4:20: 4:39 help: run `rustc --explain E0433` to see a detailed explanation 
src\main.rs:4:20: 4:39 error: unresolved name `phone::Display::new` [E0425] 
src\main.rs:4  let display = phone::Display::new(10,20); 
           ^~~~~~~~~~~~~~~~~~~ 
src\main.rs:4:20: 4:39 help: run `rustc --explain E0425` to see a detailed explanation 
src\main.rs:6:16: 6:31 error: failed to resolve. Could not find `GSM` in `phone` [E0433] 
src\main.rs:6  let gsm = phone::GSM::new("modelPhone".to_string(), "manufactor".to_string(), 100.0, "owner".to_string(), battey, display); 
          ^~~~~~~~~~~~~~~ 
src\main.rs:6:16: 6:31 help: run `rustc --explain E0433` to see a detailed explanation 
src\main.rs:6:16: 6:31 error: unresolved name `phone::GSM::new` [E0425] 
src\main.rs:6  let gsm = phone::GSM::new("modelPhone".to_string(), "manufactor".to_string(), 100.0, "owner".to_string(), battey, display); 
          ^~~~~~~~~~~~~~~ 
src\main.rs:6:16: 6:31 help: run `rustc --explain E0425` to see a detailed explanation 
src\main.rs:7:5: 7:16 error: the type of this value must be known in this context 
src\main.rs:7  gsm.print(); 

Ich verstehe nicht, Ich habe hier und in Google gesucht, aber ich habe keine Lösung gefunden. Auch bekomme ich eine Menge Warnung, dass die Methoden in phone.rs nie verwendet werden #[warn(dead_code)], jede Hilfe wird geschätzt.

+0

'Batterie' ist kein Merkmal, es ist eine Struktur. –

Antwort

9

Kurze Antwort: Sie brauchen die mod phone in phone.rs nicht. Entfernen Sie das einfach und Ihr Code funktioniert (vorausgesetzt, der Rest des Codes ist korrekt).

Lange Antwort: Der folgende Code in main.rs:

pub mod phone; 

entspricht:

pub mod phone { 
    // literally insert the contents of phone.rs here 
} 

so brauchen Sie nicht alles in phone.rs in mod phone {} zu wickeln.

Ihr aktueller Code übersetzt:

pub mod phone { 
    pub mod phone { 
     pub struct Battery { ... } 
     ... 
    } 
} 

das heißt, Sie Battery als phone::phone::Battery (und Display als phone::phone::Display, etc.) in main.rs zugreifen muß.

+0

Danke, alles funktioniert, ich gebe dir die beste Antwort, wenn ich kann. –

Verwandte Themen