# Static access to "enum" member

**URL:** <https://users.rust-lang.org/t/static-access-to-enum-member/139795>\
**Category:** uncategorized\
**Created:** [April 26, 2026, 3:32am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795 "2026-04-26T03:32:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyberphone](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cyberphone/32/51927_2.png) [@cyberphone](https://users.rust-lang.org/u/cyberphone)\
**Post date:** [April 26, 2026, 3:32am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/1 "2026-04-26T03:32:30Z")

</div>

n00b warning...  
I wanted to have a `new()` method as alternative to using a macro. Dumb idea?

```rust
trait Trait {
    fn do_stuff(&self);
}

struct Foo {
    vector: Vec<String>,
}

impl Trait for Foo {
    fn do_stuff(&self) {
        println!("Foo");
    }
}
struct Bar {}

impl Bar {}

impl Trait for Bar {
    fn do_stuff(&self) {
        println!("Boo");
    }
}

enum FooOrBar {
    Foo(Foo),
    Bar(Bar),
    Special(i64),
}

impl FooOrBar {
    fn new_bar() -> FooOrBar {
        FooOrBar::Foo(Foo {
            vector: Vec::<String>::new(),
        })
    }

    pub fn do_stuff(&self) {
        self.as_trait().do_stuff();
    }

    fn as_trait(&self) -> &dyn Trait {
        match self {
            FooOrBar::Foo(f) => f,
            FooOrBar::Bar(b) => b,
            _ => panic!("Not permitted")
        }
    }
}

fn main() {
    // Yeah, macros the "workaround"
    let a = FooOrBar::Foo(Foo {vector: Vec::<String>::new()});
    a.do_stuff();
    let f = FooOrBar::new_bar();
    let s = FooOrBar::Special(6);
    // ***DOES NOT COMPILE***
    let p = FooOrBar::Foo::new();
}

```

---

<div class="post-metadata">

**Author:** ![jumpnbrownweasel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jumpnbrownweasel/32/17403_2.png) [@jumpnbrownweasel](https://users.rust-lang.org/u/jumpnbrownweasel)\
**Post date:** [April 26, 2026, 4:01am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/2 "2026-04-26T04:01:40Z")

</div>

I probably don't understand what you're wanting to do, but does this help?

```rust
impl Foo {
    fn new() -> Self {
        Self {
           vector: Vec::new(),
        }
    }
}

fn main() {
    // ...
    let p = FooOrBar::Foo(Foo::new());
}

```

---

<div class="post-metadata">

**Author:** ![cyberphone](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cyberphone/32/51927_2.png) [@cyberphone](https://users.rust-lang.org/u/cyberphone)\
**Post date:** [April 26, 2026, 4:10am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/3 "2026-04-26T04:10:13Z")

</div>

Thanx! The idea was simply hiding (for users) a potentially a bit more elaborate constructor behind a `new()` method as shown by the sample code. That `Foo` uses a `Vec` internally, _should stay internal_.

I only need to know if static access to `enum` members is possible or not.

---

<div class="post-metadata">

**Author:** ![jumpnbrownweasel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jumpnbrownweasel/32/17403_2.png) [@jumpnbrownweasel](https://users.rust-lang.org/u/jumpnbrownweasel)\
**Post date:** [April 26, 2026, 4:11am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/4 "2026-04-26T04:11:34Z")

</div>

> [@cyberphone](#):
>
> I only need to know if static access to `enum` members is possible or not.

Not for constructing them. You can deconstruct them using patterns, however.

---

<div class="post-metadata">

**Author:** ![cyberphone](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cyberphone/32/51927_2.png) [@cyberphone](https://users.rust-lang.org/u/cyberphone)\
**Post date:** [April 26, 2026, 4:15am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/5 "2026-04-26T04:15:47Z")

</div>

I see 😪 I had hoped on something like:

```rust
impl FooOrBar::Foo {
  fn new() -> FooOrBar {
  }
}

```

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [April 26, 2026, 4:17am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/6 "2026-04-26T04:17:16Z")

</div>

> [@cyberphone](#):
>
> ```rust
> // ***DOES NOT COMPILE (don't know how to declare it either...)***
> let p = FooOrBar::Foo::new();
> 
> ```

in rust, variants of an `enum` are NOT themselvs types \[1\]. you cannot define associated functions for them.

I'm not sure what you are trying to achieve, or what you mean by "workaround", but it is common to define dedicated constructors to create specific variant of an `enum`, just like what you did with `FooOrBar::new_bar()`.

can you explain what's your goal here?

* * *

1. EDIT: techinically they are, but you cannot use them like other types, they are special kind of types

---

<div class="post-metadata">

**Author:** ![jumpnbrownweasel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jumpnbrownweasel/32/17403_2.png) [@jumpnbrownweasel](https://users.rust-lang.org/u/jumpnbrownweasel)\
**Post date:** [April 26, 2026, 4:19am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/7 "2026-04-26T04:19:53Z")

</div>

Here's some info from the [reference](https://doc.rust-lang.org/reference/items/enumerations.html?highlight=variant#enumerations) on this:

> Each variant defines its type in the [type namespace](https://doc.rust-lang.org/reference/names/namespaces.html), though that type cannot be used as a type specifier. Tuple-like and unit-like variants also define a constructor in the [value namespace](https://doc.rust-lang.org/reference/names/namespaces.html).
> 
> A struct-like variant can be instantiated with a [struct expression](https://doc.rust-lang.org/reference/expressions/struct-expr.html).
> 
> A tuple-like variant can be instantiated with a [call expression](https://doc.rust-lang.org/reference/expressions/call-expr.html) or a [struct expression](https://doc.rust-lang.org/reference/expressions/struct-expr.html).
> 
> A unit-like variant can be instantiated with a [path expression](https://doc.rust-lang.org/reference/expressions/path-expr.html) or a [struct expression](https://doc.rust-lang.org/reference/expressions/struct-expr.html). For example:

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [April 26, 2026, 4:21am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/8 "2026-04-26T04:21:25Z")

</div>

> [@cyberphone](#):
>
> I only need to know if static access to `enum` members is possible or not.

what do you mean by "access", precisely?

if you have access to the `enum`, you have access to all its variant. private or public don't apply to variants, but only the `enum` itself, if that's what you mean.

---

<div class="post-metadata">

**Author:** ![cyberphone](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cyberphone/32/51927_2.png) [@cyberphone](https://users.rust-lang.org/u/cyberphone)\
**Post date:** [April 26, 2026, 4:23am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/9 "2026-04-26T04:23:55Z")

</div>

I only wanted to create a _public_ API pattern that (in my eyes...) looks prettier (_and more logical_) than `FooOrBar::new_bar()`. imagine there are many `enum` members. Some of the members (like `Foo`) have no arguments while others like `Special` have explicit arguments, all _preferably_ following the same pattern.

Well, I you had the right to dream (...) it would be cool if you could intercept the `enum` implicit constructor so you could end-up with `FooOrBar::Foo()` 🔥

Yeah, my terminology may not be correct but I thought the example would cover that 😉

I guess my question has been answered.

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [April 26, 2026, 4:44am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/10 "2026-04-26T04:44:41Z")

</div>

> [@cyberphone](#):
>
> I only wanted to get an API pattern that (in my eyes) looks prettier (and more logical) than `FooOrBar::new_bar()`.

I see. you'll get a feel what idiomatic rust APIs look like as you write more and more code.

side note:

the term "constructor" in rust is just a convention, it's not a special language concept. any function that returns a value of type `T` can be considered a constructor for `T`. specifically, it doesn't have to be an associated function of `T` either.

```rust
/// some type
enum Foo {
  //...
}

/// this is a "constructor" for type `Foo`
fn foo() -> Foo {
    todo!()
}

impl Foo {
    /// this is another more conventional constructor, with the name being `new`
    fn new() -> Self {
        todo!()
    }
}

/// but this is also a constructor, note it's async and fallible
/// as you see, the function name doesn't really matter, and the return type
/// doesn't need to be just `Foo` either, it can be "embelished"
fn make_fancy_foo() -> impl Future<Output = Result<Self, MyError>> {
    async { todo!() }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 25, 2026, 4:44am UTC](https://users.rust-lang.org/t/static-access-to-enum-member/139795/11 "2026-07-25T04:44:48Z")

</div>

Closed on July 25, 2026, 4:44am UTC

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.
