Ancestors

Toot

Written by Esteban Küber :rust: on 2024-12-10 at 16:21

I can't wait to see how people use default field values in #Rust!

It felt like an eternity to land this (specially if we count the years of discussion before the, I believe, 3rd RFC was accepted), but as of next nightly you will be able to write

struct Foo {

bar: Type = Type::const_method(),

}

Foo { .. } // implicit bar: Type::const_method() call

The main difference between https://github.com/rust-lang/rust/pull/129514 and derive(Default) is that the latter doesn't support having mandatory fields.

[#]RustLang

=> View attached media

=> More informations about this toot | More toots from ekuber@hachyderm.io

Descendants

Written by Esteban Küber :rust: on 2024-12-11 at 17:43

Default fields values are now live on nightly #Rust! Go wild and report bugs 😃

[#]RustLang

=> View attached media | View attached media

=> More informations about this toot | More toots from ekuber@hachyderm.io

Written by Matt Campbell on 2024-12-10 at 16:25

@ekuber Ooh, this might convince me to raise my project's MSRV while the project is still pre-1.0.

=> More informations about this toot | More toots from matt@toot.cafe

Written by Owen Nelson on 2024-12-10 at 16:29

@ekuber nailed it. Very exciting.

=> More informations about this toot | More toots from onelson@mastodon.social

Written by Jonas Platte on 2024-12-10 at 16:38

@ekuber #[non_exhaustive] compatibility when? 😛

=> More informations about this toot | More toots from jplatte@social.tchncs.de

Written by Esteban Küber :rust: on 2024-12-10 at 17:46

@jplatte Coming. Didn't want to try and land the whole thing in one go. That would be a nightmare to rebase and unfair for the reviewers.

=> More informations about this toot | More toots from ekuber@hachyderm.io

Written by Jonas Platte on 2024-12-10 at 18:41

@ekuber oh yeah, of course. I was 100% joking, did not expect it to be something you're already working on! :blobcataww:

=> More informations about this toot | More toots from jplatte@social.tchncs.de

Written by Tristam on 2024-12-10 at 17:20

@ekuber that is a really nice ergonomics win

=> More informations about this toot | More toots from swiftcoder@mastodon.social

Written by Aedius Filmania ⚙️🎮🖊️ on 2024-12-10 at 18:05

@ekuber

That's so great ! 💪

What a great gift for the new year 🎉

=> More informations about this toot | More toots from Aedius@lavraievie.social

Written by Valentin C. on 2024-12-10 at 18:09

@ekuber actually WOW really nice! How does it play with default ?

Edit: just had to click the link 😅 very nice!

=> More informations about this toot | More toots from shavounet@piaille.fr

Written by Ed Page on 2024-12-10 at 19:20

@ekuber Tracking clap support for default field values in https://github.com/clap-rs/clap/issues/5839

=> More informations about this toot | More toots from epage@hachyderm.io

Written by Soso on 2024-12-10 at 19:40

@ekuber Definitely hyped for that. We have a crate for a driver with structures defined for around 50 commands, and most have a lot of optional fields.

Right now we use typed-builder to make that usable, but being able to use rust syntax would be amazing!!

https://docs.rs/se05x/latest/se05x/se05x/commands/index.html

=> More informations about this toot | More toots from sgued@pouet.chapril.org

Written by Bennolius 😷⚡ on 2024-12-10 at 23:26

@ekuber I'm new to Rust and I thought I just mustn't have worked out how to set default values yet. So this will be rather welcome.

=> More informations about this toot | More toots from carbsrule_en@polyglot.city

Written by Esteban Küber :rust: on 2024-12-11 at 04:46

@carbsrule_en in the meantime, look for typed builder crates, which aren't as ergonomic but are better than nothing.

=> More informations about this toot | More toots from ekuber@hachyderm.io

Written by Martijn Faassen on 2024-12-24 at 00:16

@ekuber

@carbsrule_en

Builders do support the incremental use case where different parts of the code do building, and cloning a builder to form a template, and such.

Maybe I haven't done enough reading but I don't think default field values can replace all builder use cases, right?

Not that they have to. I'm intrigued by the possibilities of default field values, so thinking it through.This looks like a better way to support the default parameter use case for instance.

=> More informations about this toot | More toots from faassen@fosstodon.org

Written by Esteban Küber :rust: on 2024-12-24 at 01:16

@faassen indeed. If you have a situation where you want to pass a "partially constructed" value around, then you still need the builder pattern.

=> More informations about this toot | More toots from ekuber@hachyderm.io

Written by Piggo :verified_horse: on 2024-12-11 at 19:29

@ekuber is this just a syntactic sugar for implementing Default?

=> More informations about this toot | More toots from piggo@piggo.space

Written by BD103 on 2024-12-11 at 19:39

@piggo @ekuber I believe it's more of a language feature than an extension for Default. Check out the RFC: https://rust-lang.github.io/rfcs/3681-default-field-values.html

=> More informations about this toot | More toots from bd103@hachyderm.io

Written by Piggo :verified_horse: on 2024-12-11 at 19:45

@bd103 @ekuber oh, that's a bit confusing

=> More informations about this toot | More toots from piggo@piggo.space

Written by Esteban Küber :rust: on 2024-12-11 at 20:11

@piggo You can't use traits to encode "some of these fields are mandatory". Default is all are optional. The normal state is all are mandatory. This feature sits in the middle.

=> More informations about this toot | More toots from ekuber@hachyderm.io

Written by Khionu S :trans_furr_white: on 2024-12-11 at 20:16

@ekuber Really appreciating this, it's basically eliminated all my manual Default implementations

=> More informations about this toot | More toots from khionu@hachyderm.io

Written by pedro 🍉 on 2024-12-11 at 20:42

@ekuber yay!

=> More informations about this toot | More toots from ubik@fedi.turbofish.cc

Written by Psyhackological on 2024-12-11 at 21:24

@ekuber looks nice but isn't this just less boilerplate?

=> More informations about this toot | More toots from psyhackological@fosstodon.org

Written by Esteban Küber :rust: on 2024-12-11 at 22:07

@psyhackological isn't it all? Who needs for when you have while? Who needs if let when you have match?

=> More informations about this toot | More toots from ekuber@hachyderm.io

Written by Psyhackological on 2024-12-12 at 08:52

@ekuber hmm true...

=> More informations about this toot | More toots from psyhackological@fosstodon.org

Written by Jacob Pratt on 2024-12-11 at 23:47

@ekuber Prefer issues be opened as usual, DM, or something else?

=> More informations about this toot | More toots from jhpratt@mastodon.social

Written by Esteban Küber :rust: on 2024-12-12 at 00:15

@jhpratt tickets on the issue tracker referencing the tracking issue and tagged as F-default_field_values would be best

=> More informations about this toot | More toots from ekuber@hachyderm.io

Written by Jacob Pratt on 2024-12-14 at 05:09

@ekuber Took maybe an hour to figure out how to minimize it, but the first bug for the feature is now open :)

https://github.com/rust-lang/rust/issues/134298

=> More informations about this toot | More toots from jhpratt@mastodon.social

Written by Esteban Küber :rust: on 2024-12-14 at 05:39

@jhpratt it looks like I need to extend what the Well formed check does for structs with defaults. You would think that they stretch without a default would also error.

This is a nice milestone!

=> More informations about this toot | More toots from ekuber@hachyderm.io

Written by Jacob Pratt on 2024-12-14 at 05:49

@ekuber it's heavily minimized from real-world code in the time crate (with the addition of default values). Inference is a wonderful thing!

=> More informations about this toot | More toots from jhpratt@mastodon.social

Written by mehbark on 2024-12-11 at 23:55

@ekuber i don't even use bevy, but i'm most excited for them (they've been exporting default in their prelude)

=> More informations about this toot | More toots from mehbark@critter.cafe

Written by Gábor Gyebnár on 2024-12-12 at 17:05

@ekuber So good. :) Combined with #[non_exhaustive], the ".." syntax is a now very convenient way to maintain API compatibility. Typing "..Default::default()" was driving me nuts. :)

=> More informations about this toot | More toots from aedm@mastodon.gamedev.place

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113629441681016331
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
499.117902 milliseconds
Gemini-to-HTML Time
6.583389 milliseconds

This content has been proxied by September (3851b).