2014-09-08 6 views
1

std::io에서 오류 처리가 완료되는 방식을 다시 만들려고합니다. 내 코드는 this 문서에 있습니다.열거 형 값을 볼 수 없습니다.

내 문제는 내 코드를 별도의 mod에 넣었으므로 반환하려는 열거 형 값을 볼 수 없습니다. 코드 샘플 :

mod error { 
    use std::str::SendStr; 

    pub type ProgramResult<T> = Result<T, ProgramError>; 

    #[deriving(Show)] 
    pub struct ProgramError { 
     kind: ProgramErrorKind, 
     message: SendStr 
    } 

    /// The kinds of errors that can happen in our program. 
    /// We'll be able to pattern match against these. 
    #[deriving(Show)] 
    pub enum ProgramErrorKind { 
     Configuration 
    } 

    impl ProgramError { 
     pub fn new<T: IntoMaybeOwned<'static>>(msg: T, kind: ProgramErrorKind) -> ProgramError { 
      ProgramError { 
       kind: kind, 
       message: msg.into_maybe_owned() 
      } 
     } 
    } 
} 

나는 열거 대중 정당이를 사용하려고 다른 모든 개조 수입에도 불구하고, 내 코드에 다른 곳 Configuration를 볼 수 없습니다. 어떤 아이디어?

+2

'use error :: Configuration'을 했습니까? – Levans

답변

3

enum 유형을 참조 할 때 모듈 해결 연산자를 사용하고 있습니까? 예를 들어, 잘 작동하는 예제는 다음과 같습니다.

mod error { 
    #[deriving(Show)] 
    pub enum ProgramErrorKind { 
     Configuration, 
     SomethingElse 
    } 
} 

fn main() { 
    // You can import them locally with 'use' 
    use error::Configuration; 

    let err = Configuration; 

    // Alternatively, use '::' directly 
    let res = match err { 
     error::Configuration => "Config error!", 
     error::SomethingElse => "I have no idea.", 
    }; 
    println!("Error type: {}", res); 
} 
+0

ProgramErrorKind를 가져 오는 것으로 충분하다고 생각 했습니까? – ruipacheco

+0

@lapinrigolo'use error :: {ProgramErrorKind, Configuration, SomethingElse};는 당신이 원하는 것입니다. – Manishearth