Can't import a module from another crate - unresolved import

前端 未结 1 1733
离开以前
离开以前 2021-01-26 23:14

I am trying to write a crate called bar, the structure looks like this

src/
├── bar.rs
└── lib.rs

My src/lib.rs looks

1条回答
  •  温柔的废话
    2021-01-27 00:06

    The important part you are missing is that crates define their own module. That is, your crate bar implicitly defines a module called bar, but you also have created a module called bar inside that. Your struct resides within this nested module.

    If you change your main to use bar::bar::baz; you can progress past this. You will have to decide if that's the structure you want though. Most idiomatic Rust projects would not have the extra mod and would flatten it out:

    src/lib.rs

    pub struct Baz {
        // stuff
    }
    
    impl Baz {
        // stuff
    }
    

    Unfortunately, your example code cannot compile, as you have invalid struct definitions, and you call methods that don't exist (new), so I can't tell you what else it will take to compile.

    Also, structs should be PascalCase.

    0 讨论(0)
提交回复
热议问题