55

How do I declare a "static" field in a struct in Rust, preferably with a default value:

struct MyStruct {
    x: i32,               // instance
    y: i32,               // instance
    my_static: i32 = 123, // static, how?
}

fn main() {
    let a = get_value();
    if a == MyStruct::my_static {
        //...
    } else {
        //...
    }
}
hippietrail
  • 15,848
  • 18
  • 99
  • 158
Incerteza
  • 32,326
  • 47
  • 154
  • 261

3 Answers3

104

You can declare an associated constant in an impl:

struct MyStruct {
    x: i32,
    y: i32,
}

impl MyStruct {
    const MY_STATIC: i32 = 123;
}

fn main() {
    println!("MyStruct::MY_STATIC = {}", MyStruct::MY_STATIC);
}
aitvann
  • 1,289
  • 2
  • 8
  • 10
  • 4
    `static` and `const` are [different things in Rust](https://doc.rust-lang.org/book/first-edition/const-and-static.html). – Shepmaster Feb 26 '18 at 13:03
  • 18
    @ Shepmaster, I know. I think the author of the question had in mind not static which is in Rust, but a member of the structure that is not stored in the object of the structure – aitvann Mar 01 '18 at 19:19
  • 5
    https://doc.rust-lang.org/book/const-and-static.html for a working `different things in Rust` link – Kristianmitk Jun 09 '19 at 21:48
  • @aitvann if the struct is declared static, will it apply to its members? – user2284570 Dec 13 '20 at 10:02
  • 1
    This precisely matched my use-case of "constant Python class attribute". – MisterMiyagi Feb 03 '21 at 11:53
  • 1
    This should be the accepted answer as it does what OP wants – SharedRory Oct 21 '21 at 17:36
  • I don't think it's "appropriate" for me to flag this saying that a mod should "change" this to accepted answer, given that isn't possible, but I agree. This should be made the accepted answer. – CATboardBETA May 24 '22 at 21:13
22

Rust does not support static fields in structures, so you can't do that. The closest thing you can get is an associated method:

struct MyStruct {
    x: i32,
    y: i32,
}

impl MyStruct {
    #[inline]
    pub fn my_static() -> i32 {
        123
    }
}

fn main() {
    let a = get_value();
    if a == MyStruct::my_static() {
        //...
    } else {
        //...    
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • 3
    do you have any idea why not? – Incerteza Oct 24 '14 at 15:15
  • 12
    @AlexanderSupertramp, probably because they are not really needed? Static fields can only used for scoping and encapsulation, but encapsulation unit in Rust is module, so just make a `static` in the module your struct is in, that's it. – Vladimir Matveev Oct 24 '14 at 15:18
12

You can't declare a field static in a struct.

You can declare a static variable at module scope like this :

static FOO: int = 42;

And you can't have a static variable mutable without unsafe code : to follow borrowing rules it would have to be wrapped in a container making runtime borrowing checks and being Sync, like Mutex or RWLock, but these cannot be stored in static variable as they have non-trivial constructors.

Levans
  • 14,196
  • 3
  • 49
  • 53
  • 1
    If you are looking to make a static variable mutable with `Mutex` you can combine it with [`lazy-static!`](https://crates.io/crates/lazy_static) (which does additional wrapping of its own) – jkmartindale Jun 27 '19 at 16:46
  • @Levans, if the struct is declared static, will it apply to its members? – user2284570 Dec 13 '20 at 10:02