New features for struct and traits

I would like to see in rust readonly struct, variables and default values de like in c#. It should look something like this.

readonly struct Data {
    number: 'init f32,
    text: 'default &str = "world",
    date: 'default datetime = datetime::current(),
}

And also use to declare constants of a function will arguments in the form of constants or values, the result of which will be known at compile time. I would also like to have groups of variables or constants, this will make life easer and reduce the digging in the the vars. It should look something like this.

group main_vars {
    group strings {
        string1: &str,
        string2: &str,
        string3: &str,
    }
    group ints {
        int1: i32,
        int2: i32,
    }
    group etc {
        string: str,
        int: i32,
    }
}

still wouldn't mid adding the ability of writing traits in struct. for example so.

struct Name {
    trait {
        //functions
    }
    impl Trait {
        //implement
    }
}

Read-only fields will, in a way, be coming on nightly very soon. Default field values is an RFC I'm coming back around to soon. Declaring an argument is const can already be done to an extent with const generics.

The last two, don't hold your breath. I'd strongly oppose either.

It's important to look at why readonly fields exist in C#: because there's no aliasing restrictions. And because you have multi-path mutable access to fields by default in C#, it's helpful to overestimate the "don't do that" as "make it immutable" -- basically the functional programming solution.

But Rust says that you don't actually need it immutable, you just need to not let it change when someone other than whoever changed it could notice.

In Rust, you just pass &Data and the whole thing is immutable (assuming you didn't use interior mutability). It's like in Data data as a parameter in C#, but more general and more reliable: You can use it with everything, not just special stuff.

10 Likes

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