我试图在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
,即使枚举是公开的,并且在尝试使用它的所有其他mod中正确导入。有什么想法吗?
答案 0 :(得分: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);
}