2015-06-05 102 views
9

src/lib.rs我有以下如何從兄弟模塊導入?

extern crate opal_core; 

mod functions; 
mod context; 
mod shader; 

然後在src/context.rs我有這樣的事情,它試圖從src/shader.rs符號輸入:

use opal_core::shader::Stage; 
use opal_core::shader::Shader as ShaderTrait; 
use opal_core::GraphicsContext as GraphicsContextTrait; 

use functions::*; // this import works fine 
use shader::*; // this one doesn't 

pub struct GraphicsContext { 
    functions: Gl 
} 

fn shader_stage_to_int(stage: &Stage) -> u32 { 
    match stage { 
     &Stage::Vertex => VERTEX_SHADER, 
     &Stage::Geometry => GEOMETRY_SHADER, 
     &Stage::Fragment => FRAGMENT_SHADER, 
    } 
} 

impl GraphicsContextTrait for GraphicsContext { 

    /// Creates a shader object 
    fn create_shader(&self, stage: Stage, source: &str) -> Box<ShaderTrait> { 
     let id; 

     unsafe { 
      id = self.functions.CreateShader(shader_stage_to_int(&stage)); 
     } 

     let shader = Shader { 
      id: id, 
      stage: stage, 
      context: self 
     }; 

     Box::new(shader) 
    } 
} 

的問題是,該聲明use shader::*;給出了錯誤未解決的進口

我讀的文檔和他們說use語句總是從當前箱(opal_driver_gl)的根去,所以我認爲shader::*應該導入opal_driver_gl::shader::*但它似乎沒有這樣做。我需要在這裏使用selfsuper關鍵字嗎?

如果你可以幫忙,謝謝。

+0

你看過任何[提及相同錯誤的其他問題](http://stackoverflow.com/search?q=% 5Brust%5D +未決+進口)?如果是這樣,你的問題與他們有什麼不同?你有沒有嘗試製作一個[小型測試用例](/ help/mcve)? – Shepmaster

+0

我已經簽出了大部分「未解決的導入」問題。他們大多圍繞着從箱子外面得到符號,但我想做相反的事情。我會盡量縮小問題的範圍。 – neon64

+1

告訴我們您嘗試了什麼以及您遇到了哪些問題是一種很好的做法。還包括爲什麼這些嘗試和問題不起作用或者你不瞭解他們。這可以防止我們猜測你真正的問題是什麼,讓你更容易得到答案,並且通常會提高你的問題對未來的搜索者的有用程度。 – Shepmaster

回答

10

要導入在同一水平上的模塊,執行下列操作:

random_file_0.rs

// Note how this is a public function. It has to be in order to be 
// usable from other files (in this case `random_file_1.rs`) 
pub fn do_something() -> bool { 
    true 
} 

random_file_1.rs

use super::random_file_0; 

#[test] 
fn do_something_else() { 
    assert!(random_file_0::do_something()); 
} 

或替代random_file_1.rs

// This can be a public function, but does not have to be unless you 
// are using it somewhere else 
use ::random_file_0; 

#[test] 
fn do_something_else() { 
    assert!(random_file_0::do_something()); 
} 

lib.rs

mod random_file_0; 
mod random_file_1; 

請參閱此鏈接:Rust By Example的更多信息和示例。如果這不起作用,這裏是它顯示的代碼:

fn function() { 
    println!("called `function()`"); 
} 

mod my { 
    pub fn indirect_call() { 
     // Let's access all the functions named `function` from this scope 
     print!("called `my::indirect_call()`, that\n> "); 

     // `my::function` can be called directly 
     function(); 

     { 
      // This will bind to the `cool::function` in the *crate* scope 
      // In this case the crate scope is the outermost scope 
      use cool::function as root_cool_function; 

      print!("> "); 
      root_cool_function(); 
     } 

     { 
      // `self` refers to the current module scope, in this case: `my` 
      use self::cool::function as my_cool_function; 

      print!("> "); 
      my_cool_function(); 
     } 

     { 
      // `super` refers to the parent scope, i.e. outside of the `my` 
      // module 
      use super::function as root_function; 

      print!("> "); 
      root_function(); 
     } 
    } 

    fn function() { 
     println!("called `my::function()`"); 
    } 

    mod cool { 
     pub fn function() { 
      println!("called `my::cool::function()`"); 
     } 
    } 
} 

mod cool { 
    pub fn function() { 
     println!("called `cool::function()`"); 
    } 
} 

fn main() { 
    my::indirect_call(); 
} 
+0

感謝您的寶貴信息,不幸的是我已經瞭解了基本知識。 @DK我認爲已經找到了問題,因爲我使用了週期性的glob導入。 (我來自Java世界,其中'import myPackage。*;'很好) – neon64