Menu

In praise of exhaustive destructuring

Hacker News·4 months ago
#XlXkHmme
Reading 0:00
15s threshold

When I learned Rust, I was frustrated by the fact that one had to list all fields or use the .. syntax when destructuring a struct.

To illustrate this and because I am writing this during a heatwave, let's suppose we have the following struct:

struct WeatherReading { station_id: String, recorded_at: DateTime<Utc>, temperature: f64 , humidity: f64 , pressure: f64 , }

Note : Except for recorded_at , all fields should be newtypes . Not done here for brevity.

Now I just want to retrieve station_id and recorded_at , why should I need to write:

let WeatherReading { station_id, recorded_at, .. } = weather_reading;

In TypeScript, I could just do:

const { station_id, recorded_at } = weather_reading;

And in Haskell:

-- With NamedFieldPuns extension let WeatherReading { station_id, recorded_at, } = weather_reading -- Or with RecordWildCards let WeatherReading { ..…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.