#How to make multiple files use a common type, or 'import' the same file?

19 messages · Page 1 of 1 (latest)

golden condor
#

main.rs

mod error;
use error::Error;

mod lib;
use lib::lib_function;


fn main() -> Result<(), Error>{
    
    lib_function()?;
//                ^ the trait `From<lib::error::Error>` 
//                  is not implemented for `error::Error`
    Err(Error{})
}```
error.rs
```rust
#[derive(Debug)]
pub struct Error;

lib.rs

#[path = "error.rs"]
mod error;
use error::Error;

pub fn lib_function() -> Result<(), Error>
{
    Err(Error{})
}

I've been searching how to "import" different rs files and mod was the only thing I found. But the files don't realize that they are using the same type.
lib::error::Error vs (main)::error:Error

rror[E0277]: `?` couldn't convert the error to `error::Error`
  --> src\main.rs:10:19
   |
8  | fn main() -> Result<(), Error>{
   |              ----------------- expected `error::Error` because of this
9  |
10 |     lib_function()?;
   |                   ^ the trait `From<lib::error::Error>` is not implemented for `error::Error`
fleet yoke
#

General rule about rust modules: if you need to use #[path] you're doing it wrong

#

(this is not always true, but the only cases when it isn't are ones when you know what you're doing in detail)

#

mod doesn't import a module, it defines it. rust doesn't automatically have modules, even if a foo.rs file exists, unless they're defined.

If you have two mod declarations that correspond to the same file, those are two distinct modules which happen to have identical source, to the compiler. Types in one of them are not the same as types in the other one

#

"Importing" things is done with use

primal garden
#

If you want both a lib and a bin, your lib items will be found under the crate name. So in main.rs, you can access them like

use crate_name::Error;
```You also need to make it public in `lib.rs`.
fleet yoke
#

Ah yeah, currently you have an error module in the library and an error module in the binary and these aren't the same module.

#

Even though they are the same file.

#

Advice: put your binary stuff inside src/bin/, to avoid confusing it with your library stuff. Also note that, as Seaish explained, you can refer to the lib from the bin as crate_name (the crate name is defined in Cargo.toml)

golden condor
#

Im sorry, i didnt mean to make a libary, only an executeable. lib.rs wasn't chosen deliberatly

modest bone
#

ok, you should rename lib.rs then

#

lib.rs is a special name like main.rs is

golden condor
#

How do I then use Error in the file?

modest bone
#

use crate::error::Error;

primal garden
#

In src/main.rs:

mod error;
mod whatever_you_change_lib_to;
```In `src/whatever_you_change_lib_to.rs`:
```rs
// either this
use crate::error::Error;
// or this
use super::error::Error;

pub fn lib_function() -> Result<(), Error>
{
    Err(Error{})
}
```In `src/error.rs`:
```rs
#[derive(Debug)]
pub struct Error;
modest bone
#

that's how you refer to a module elsewhere in the crate

fleet yoke
#

You could technically always type the full path, but useing it once is nicer

#

It's shorter overall