2015-06-05 6 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::* आयात होते रहना चाहिए, लेकिन यह ऐसा करने के लिए प्रकट नहीं होता। क्या मुझे यहां self या super कीवर्ड का उपयोग करने की आवश्यकता है?

धन्यवाद अगर आप मदद कर सकते हैं।

+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: अधिक जानकारी और उदाहरण के लिए Rust By Example:

mod random_file_0; 
mod random_file_1; 

यह लिंक देखें।

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

महान जानकारी के लिए धन्यवाद, दुर्भाग्य से मैं मूल बातें समझ चुका हूं। @ डीके मुझे लगता है कि इस मुद्दे को मिला है क्योंकि मैंने चक्रीय ग्लोब आयात का उपयोग किया था। (मैं एक जावा दुनिया से आया था जहां 'मेरा पैकेज आयात करें। *;' ठीक है) – neon64

संबंधित मुद्दे