# Exploding structs

**URL:** <https://internals.rust-lang.org/t/exploding-structs/13884>\
**Category:** Uncategorized\
**Created:** [January 22, 2021, 2:08pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884 "2021-01-22T14:08:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 22, 2021, 2:08pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/1 "2021-01-22T14:08:48Z")

</div>

We'd like to see this in Rust one day:

```rust
#[derive(PartialEq, Eq)]
struct X {
  foo: i32,
  bar: i32,
}

#[derive(PartialEq, Eq)]
struct Y {
  bar: i32,
  baz: i32,
}

let y = Y { bar: 3, baz: 5 }
let x = X { bar: 1, Y { bar: foo, ..: _ }: y };
assert_eq!(x, X { foo: 3, bar: 1 });

```

This would be basically a merger of FRUs with struct unpacking. It's basically sugar tho, as you can do this today:

```rust
let y = Y { bar: 3, baz: 5 }
let Y { bar: y_bar, baz: y_baz } = y;
drop(y_baz);
let x = X { bar: 1, foo: y_bar };

```

but it would make sense if we ever got subslice subpatterns (from [Uh, what ever happened to subslice patterns? - #7 by Soni](https://internals.rust-lang.org/t/uh-what-ever-happened-to-subslice-patterns/13882/7) )

---

<div class="post-metadata">

**Author:** ![elidupree](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/elidupree/32/4304_2.png) [@elidupree](https://internals.rust-lang.org/u/elidupree)\
**Post date:** [January 22, 2021, 3:18pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/2 "2021-01-22T15:18:19Z")

</div>

So, if I understand correctly, your suggestion is to allow pattern-matching inside {} constructors, and any names that are bound in the pattern are assigned into the field of the same name?

The code from your example doesn't seem very readable, but I could imagine wanting something like this:

```rust
fn some_fn() -> (i32, i32) { ... }

let x = X { (foo, bar): some_fn() };

```

That seems readable enough. On the other hand, I don't see a particularly strong motivation for it. In current Rust, I would write this:

```rust
fn some_fn() -> (i32, i32) { ... }

let (foo, bar) = some_fn();
let x = X { foo, bar };

```

---

<div class="post-metadata">

**Author:** ![atagunov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/atagunov/32/5877_2.png) [@atagunov](https://internals.rust-lang.org/u/atagunov)\
**Post date:** [January 22, 2021, 3:19pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/3 "2021-01-22T15:19:55Z")

</div>

Immediate reaction: couldn't figure what this means in 3 minutes of pondering..  
It just keep hurting my brain trained on C and Java.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [January 22, 2021, 4:40pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/4 "2021-01-22T16:40:25Z")

</div>

A previous conversation about a related feature: [Pre-RFC: Array expansion syntax](https://internals.rust-lang.org/t/pre-rfc-array-expansion-syntax/13490)

Ignoring the mentioned syntax, this fits the sort of general "splatting" idea. Like I could imagine splatting a 2D point into a 3D point, providing the third coordinate explicitly.

---

<div class="post-metadata">

**Author:** ![gbutler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gbutler/32/3670_2.png) [@gbutler](https://internals.rust-lang.org/u/gbutler)\
**Post date:** [January 22, 2021, 6:22pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/5 "2021-01-22T18:22:23Z")

</div>

> [@Soni](#):
>
> We'd like to see this in Rust one day:

Why? What's the motivating example. What can be achieved that can't already?

Also, it just seems incredibly confusing and unreadable. I'd never want to work on code like this. Reminds me of all the arguments of why Perl was superseded by Python.

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 22, 2021, 8:07pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/6 "2021-01-22T20:07:01Z")

</div>

Mostly to unify a bunch of stuff under a common syntax and RFC. See also the other linked discussions.

---

<div class="post-metadata">

**Author:** ![gbutler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gbutler/32/3670_2.png) [@gbutler](https://internals.rust-lang.org/u/gbutler)\
**Post date:** [January 22, 2021, 8:49pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/7 "2021-01-22T20:49:28Z")

</div>

> [@Soni](#):
>
> to unify a bunch of stuff under a common syntax and RFC

I'm not sure that qualifies as a motivating goal. I don't think "unifying syntax" in and of itself has ever been recognized as a worthy goal in Rust. Is there some motivating example that demonstrates the utility of this feature beyond the status quo?

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 22, 2021, 9:22pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/8 "2021-01-22T21:22:34Z")

</div>

Personally we want the following features to be unified and defined under a single RFC:

1. "Splatting" (array/tuple concatenation)

2. Subslice subpatterns

3. Current FRU syntax would be deprecated in favor of `..:` for consistency with the previous 2 features.

It might make sense _not_ to go "all in" and have full-on exploding structs. Rust devs do tend to be conservative about these things. But, exploding structs are simply a further extension of these features from our wishlist. Maybe something to put into a "future work" section of an RFC.

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [January 23, 2021, 3:36am UTC](https://internals.rust-lang.org/t/exploding-structs/13884/9 "2021-01-23T03:36:19Z")

</div>

"Splatting" and subslice subpatterns are features that I would like to see in Rust, although the syntax you proposed doesn't work due to ranges and type ascription. A better syntax might be:

```rust
// splatting
let foo = [4, 5];
let bar = [1, 2, 3, foo @ ..];

// subslice subpattern
let [[a, b, c] @ .., rest @ ..] = bar;

```

(Although that would be somewhat inconsistent with other pattern syntax)

That being said, I don't see a strong motivation for allowing something similar for structs.

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 23, 2021, 12:20pm UTC](https://internals.rust-lang.org/t/exploding-structs/13884/10 "2021-01-23T12:20:54Z")

</div>

Type ascription has no use with RangeFull so that seems like it's fine to special case for.

---

<div class="post-metadata">

**Author:** ![ratijas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ratijas/32/6756_2.png) [@ratijas](https://internals.rust-lang.org/u/ratijas)\
**Post date:** [January 28, 2021, 10:00am UTC](https://internals.rust-lang.org/t/exploding-structs/13884/11 "2021-01-28T10:00:52Z")

</div>

> ```rust
> let x = X { bar: 1, Y { bar: foo, ..: _ }: y };
> 
> ```

That's kinda even more cursed than regular expression one-liners. Each such line of code would require a block comment above explaining what's really going on, and a bunch of assertions/tests below to make sure nothing's screwed up.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 28, 2021, 10:01am UTC](https://internals.rust-lang.org/t/exploding-structs/13884/12 "2021-04-28T10:01:45Z")

</div>

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