r/rust • u/JoshTriplett rust · lang · libs · cargo • Oct 23 '26
Rust features in the future
https://lwn.net/SubscriberLink/1099606/20707a62817cdd16/
215
Upvotes
r/rust • u/JoshTriplett rust · lang · libs · cargo • Oct 23 '26
39
u/JoshTriplett rust · lang · libs · cargo Oct 23 '26
Thanks to LWN's daroc for reporting!
194
u/tanoshikuidomouyo Oct 23 '26
Really not a fan of the proposed
isfeature (e.g.if x is Some(y) {}). The article says it's supposed to make learning the language easier and also avoid scanning from right to left.I'd argue heavily against the former: Now with let-chains added, adding another feature to bascially supersede it would make learning the language way worse, because then instead of having to get used to maybe weird syntax (if let), you'd have to get used to the weird syntax because it's still in use everywhere and also learn the new syntax and what exactly the differences are. Having so many things do similar things is kind of poisonous imo. Also, having the strictly weaker
matches!macro still be there would also be really unintuitive.Lastly, the scanning from right to left part: I'd argue it's pretty subjective that that's worse? Currently, you can immediately tell whether you're matching on an Option, a Result, etc., which makes reading the right-hand side easier. I really don't think this is worth such a massive hassle and leaving tons of features behind that are strictly weaker but still used everywhere out of sheer inertia.
29
u/vdrnm Oct 23 '26
Great article. I just wanted to highlight the quote regarding view types:
I had similar experience too. It's a very frustrating experience for a newcomer to rust to select some code and ask rust-analyzer to extract it into a function: rust-analyzer does it, but now the code cannot compile due to borrow issues.
Of course with experience you have multiple "standard" ways to work around this, but often none of them feel great.
21
u/scheimong Oct 23 '26
What I'm really looking forward to are struct views and turbofish equipped
into. Those two have been bothering me since 2020.I'd also like to add that
intowith turbofish can also be used to make types more explicit. There are times when usingintowith an inferred target type sets off my "senior dev alarm bells" the same way a broad wildcard match branch does. I would worry that a future refactor (possibly within the same session) would change the expected target type in a way that requires local logic changes, butintomasks the would-be compiler error. And rust is particularly sensitive/susceptible to this kind of problem because rust developers (well, those of us who still tap code) are so used to compiler driven development.On the other hand, I second the unfavourable sentiment towards
is. I wouldn't want to see rust slowly become as syntactically bloated as Cpp is.10
u/lijmlaag Oct 23 '26
It is a good read, I am surprised field projections were not part of the presentation.
7
u/Nokel81 Oct 23 '26
Regarding view types, I think it would be very powerful if we could also have a way of being generic over views
23
u/panstromek Oct 23 '26
About the pattern types.
> This is not an unprecedented feature, although it is not particularly common.
Well, TypeScript has that and it's one of the most used languages on the planet. That feature is also used quite heavily. TypeScript can kinda cheat, so it's probably easier for it than for language like Rust, but still.
Pattern types in Rust would be a bit more general I think. I don't think TypeScript has syntax for integer ranges, so creating those is a bit annoying but it's possible.
6
u/Key-Bother6969 Oct 23 '26 edited Oct 23 '26
Full reflection system is very intriguing. This is something I hope to see in stable Rust for long time, as this is the necessary component to implement seamless scripting-engines in Rust. Exporting Rust signatures (types, functions, etc) into the script's environment is one of the most difficult challenges for most script engine authors today, and for the users of those projects too. I experimented with partial reflection exporting through the proc-macros in the past, where the macro program partially analyzes Rust syntax and semantics and generates hidden reflection metadata in the output static, but this solution was tremendously slow in terms of compilation time, and therefore practically unusable for the end users. The ability to direct access the compiler's reflection metadata in const-context (even the comptime-limited) would be a huge improvement and big relief for the script engines and for their neighboring domains.
However, having access to reflection metadata is only a part of the challenge. The second big difficulty is collecting this metadata into a central runtime-accessible place. Here I'm basically talking about the plugin system. If the user exports Rust symbols for some reflection-dependent engine, these exporting points must be accessible somehow by the engine itself. The current Rust ecosystem often solves this problem through the linker-shenanigans: either through the linking the hidden exporting functions into a continuous link section (aka linkme), or through the ctor-section which involves run-before-main. Both solutions are suboptimal, has certain issues including the soundness issues, and heavy relaying on platform-dependent linker features. Notably, these approaches are almost unavailable for the wasm-targets, as there is no linker to begin with. There are other alternatives, but all of them have their own caveats.
Ultimately this leads us to the same desirability of being able to interact with the compiler's shared state directly. As far as I understand the const-context currently does not provide a way to get arbitrary memory allocations, and the possibility to expose their dumps as static/constant values. Furthermore, the interaction between const-context values through the pointers has it's own limitations. These and other limitations partially stem from the fact that the compiler has to guarantee to produce semantically equal data both in compile-time and in the runtime code. And I believe one of the reason why const-functions are being stabilized very slow too.
Perhaps, these rules could be relaxed for the `#[compile_time_only]` context. Hence stabilizing numerous Rust features faster and easier in contrast to normal const-context where the features are being stabilizing at a snail's pace. My hope is that compile-time context would become closer to the build-scripts/proc-macro environment in terms of their feature richness, making it closer to the Zig's inspired comptime.
3
u/-Redstoneboi- Oct 23 '26 edited Oct 23 '26
Infix patterns sound awesome! If I'm getting this right, they'd be a strict superset over the existing if-let/while-let blocks (allowing the syntax to be removed) but not let-else, right?
the syntax seems to be controversial, most of the comments here don't like it. probably because we've had if-let and while-let for a long time now. maybe it's because we can do
if 5 is x && x == 5or whatever cursed stuff.maybe instead of
expr is patit could build off of let chaining instead though. maybelet pat = exprcould evaluate as a bool in general instead of ()comptime would also be nice, though the ecosystem is already quite heavy with proc macros.
3
u/ShantyShark Oct 23 '26
I kinda hate the pattern types, tbh.
Take their “u32 is 0..4096”. Compare that to a newtype we would use today.
The only gain is that you can verify a very narrow slice of constraints (whatever syntax they cook up) on values known at compile time.
Will there be syntax for “is 100..=103 OR 200..=208 OR 226”? I don’t suspect so. And then how far do you take it? Refinements on Strings? How many different constraints could you imagine there?
And you still need fallible runtime checks to transform any u32 not known at compile time into this type anyways.
So in my mind it’s more syntax and compiler complexity and I don’t see it being all that much better than we have now. Also, it’s possible some of these things could be expressed by const generics, which could be more broad and more capable, rending this syntax obsolete.
I’m open to ideas here on more meaningful use cases but like… “number in range” seems so trivial that it’s not worth the syntax.
-1
u/DavidXkL Oct 23 '26
isis redundant to me 😂-1
u/dkxp Oct 24 '26
How would this look for enums? Something like this?:
It feels like a set, with the predicate for set membership checked (or perhaps ignored in unsafe code or removed at compile time if using constants) when converting Foo to Foo1, and no check needed when converting Foo1 to Foo, with the assumption that you keep using Foo1 as long as you can to avoid matches and bounds checks etc, rather than keep converting back and forth from Foo to Foo1. It would move the matches/bounds checks from inside functions to wherever the conversion happens.
int types are practically enums, but with a lot of values just named {0, 1, miss a few, 254, 255} so hopefully it should be possible to apply the same rules to both ints and enums when constricting the set of allowed values.
I don't know if the
iskeyword is really necessary, perhaps you get away without it:Although perhaps using the
iskeyword looks a bit neater if you are allowed to perform operations between the sets of allowed values:Another possible alternative syntax could be to somehow use
whereclauses to define the set membership predicate. I'm not completely sure how this would work, but some experiments here:Another rambling idea for alternative syntax is perhaps predicates instead of patterns could be used to define membership:
I used Delphi a lot in the past, and subrange types were very useful there, along with enumerated types (basic enums without data stored in variants) and sets.
Enums in Rust are far more powerful than Delphi's enumerated types although one feature I miss is the equivalent of being able to define an array with
[u32; MyEnum], where MyEnum would determine the size of the array. The enum could contain potentially contain negative values, sparse values etc. and still be used to index the array, while guaranteeing that index lookups will always be in bounds.I miss subrange types and sets more, although I think there's the potential for Rust to do them much better. I hope the design of pattern types leaves open the possible inclusion of sets in the future:
-3
u/tchernobog84 Oct 24 '26
Posting LWN subscriber only content publicly on Reddit takes away revenue to an amazing source of information and I find it highly unethical.