# Pub impl Type syntax

**URL:** <https://internals.rust-lang.org/t/pub-impl-type-syntax/10662>\
**Category:** language design\
**Created:** [August 3, 2019, 3:03pm UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662 "2019-08-03T15:03:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LunarLambda](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lunarlambda/32/11377_2.png) [@LunarLambda](https://internals.rust-lang.org/u/LunarLambda)\
**Post date:** [August 3, 2019, 3:03pm UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662/1 "2019-08-03T15:03:38Z")

</div>

We already have the ability to have multiple, separate `impl` blocks, and for large (non-trait) `impl`s writing `pub` over and over is repetitive.

A `pub impl` (and `pub(...) impl`) syntax would allow an immediate, clear distinction between public and private implementation:

```rust
pub struct MyStruct;

pub impl MyStruct {
    // Public API here
    // behaves as if all functions were `pub fn`
}

impl MyStruct {
    // Private implementation here
}

```

This should (as far as I can tell) be perfectly backwards compatible, as one could still use the regular `impl` syntax and make individual functions `pub`.

This syntax would be symmetrical to `pub trait`.

A potential issue is that for sufficiently large `impl`s, it would be unclear if a given function is actually public or not without looking at the top of the block.

However, with our documentation tools this should not be a problem. Furthermore, it should be no issue for IDEs to show the visibility again as part of a tooltip or similar facility.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [August 3, 2019, 4:56pm UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662/2 "2019-08-03T16:56:30Z")

</div>

The main problem is that this also would imply `pub impl Trait for Type` and more problematically, `pub(crate) impl Trait for Type`, which is [a bit more problematic](https://github.com/rust-lang/rfcs/pull/2529).

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [August 3, 2019, 8:39pm UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662/3 "2019-08-03T20:39:17Z")

</div>

It doesn’t seem like a massive inconsistency to disallow `pub impl Trait for Type` given that you already can’t use `pub fn` inside an `impl Trait for Type`.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [August 4, 2019, 12:26am UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662/4 "2019-08-04T00:26:52Z")

</div>

I don't see much value in this. It's not that hard to write `pub` once per public function.

I don't think it'd make distinction clearer, because locally `fn foo()` wouldn't say whether it's public or not, so the distance between implementation and nearest `pub` would be even greater.

For the record I don't like that `impl Trait for T` forbids `pub fn`. If anything, I'd prefer allowing `pub` in trait implementations, so that public functions without `pub` can be avoided.

---

<div class="post-metadata">

**Author:** ![JustAPerson](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/justaperson/32/3166_2.png) [@JustAPerson](https://internals.rust-lang.org/u/JustAPerson)\
**Post date:** [August 4, 2019, 3:41am UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662/5 "2019-08-04T03:41:17Z")

</div>

This makes code harder to read: you can no longer determine if a method is public just by looking at it, you may also have to scroll up to top of the impl block.

We should optimize for the reader rather than the writer.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [August 4, 2019, 4:02am UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662/6 "2019-08-04T04:02:43Z")

</div>

It's not as simple as not having this is simpler on the reader.

If an `impl` block could be declared `pub`, it would be easier for a reader of the file to load "all functions in this block are public" than to check each individual function for being public.

Now, if the reader is jumping straight to the function declaration rather than reading the whole block, visibility on the function is indeed easier to load.

I'd support `$vis impl Type` iff we also have `$vis impl Trait for Type`. That would bring the two closer together conceptually.

Here's a question for @LunarLambda :

What does a lower visibility function within a more visible `impl` do? Do we allow it? What about a higher visiblity function in a less visible `impl`?.

These aren't questions with obviously correct answers -- is the `impl` block itself what you're changing the visibility of? -- and I think getting it correct would be too tied to `$vis impl Trait for Type` to do this without knowing how we want to handle that (even if it's to say "it won't work"). Having `$vis impl` work differently in the two cases would be very problematic. The difference between the two `impl` blocks' visibility behavior is already unfortunate enough.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [August 4, 2019, 5:51am UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662/7 "2019-08-04T05:51:35Z")

</div>

I think this is a bad idea, not only because of the readability problem (which would be a huge problem in itself too), but because of the unintended and non-obvious interactions mentioned in the previous post. I don't think we should complicate the visibility system at all, it's good as-is.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 2, 2019, 6:05am UTC](https://internals.rust-lang.org/t/pub-impl-type-syntax/10662/8 "2019-11-02T06:05:09Z")

</div>

Closed on November 2, 2019, 6:05am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
