Pub(crate) impl ThirdTrait for FourthStruct{....}

pub(crate) impl ThirdCrateTrait for FourthCrateStruct{....} ,Will rust support this?

I usually write code like this:
use third::ThirdCrateTrait ;
use fourth::FourthCrateStruct;
struct MyStruct(fourth::FourthCrateStruct)
impl ThirdCrateTrait for MyStruct{...}

if rust support this : pub(crate) impl ThirdCrateTrait for FourthCrateStruct{....} ,This will simplify a lot.

This works in a single crate, if that's what you mean. If it's not, I don't understand the question.

Trait implementations are always public, and thus don't support a pub(...) visibility qualifier.


Forum tip: Wrap your code in ```s.

```
(code goes here)
```

:arrow_down:

pub mod third {
    pub trait ThirdTrait {}
}

pub mod fourth {
    pub struct FourthCrateStruct;
}

struct MyStruct(fourth::FourthCrateStruct);
impl third::ThirdTrait for MyStruct {}

Why not do one of these?

mod solution1 {
    pub mod third {
        pub trait ThirdTrait {}
    }

    pub mod fourth {
        pub struct FourthCrateStruct;

        impl super::third::ThirdTrait for FourthCrateStruct {}
    }
}

mod solution2 {
    pub mod third {
        pub trait ThirdTrait {}

        impl ThirdTrait for super::fourth::FourthCrateStruct {}
    }

    pub mod fourth {
        pub struct FourthCrateStruct;
    }
}

mod solution3 {
    pub mod third {
        pub trait ThirdTrait {}
    }

    pub mod fourth {
        pub struct FourthCrateStruct;
    }

    use fourth::FourthCrateStruct;
    use third::ThirdTrait;
    impl ThirdTrait for FourthCrateStruct {}
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.