You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Types will be written Box<Foo> and not box Foo even if box_patterns is stabilized. Allowing box Foo would be a new feature that is not planned I think. box is useful to box values (shorter then Box::new(value) and allows some pattern matching impossible before.
Oh, I understand that in general, box Foo won't be a type. I was just wondering because I was under the assumption that &self in method definitions was more of a pattern than a type description, which means that box self in methods makes sense.
&self in methods does not work like a pattern. For example in fn foo(x: &u32) { let &y = x } the &y pattern "removes" one pointer indirection and make y have type u32, whereas in a impl SomTrait for u32 { fn bar(&self) { … }} method self has type &u32 with the &_ indirection included.
error: box pattern syntax is experimental(see issue #29641)
--> a.rs:2:9
|
2 | let box a = Box::new(1);
| ^^^^^
|
= help: add #![feature(box_patterns)] to the crate attributes to enable
error: aborting due to previous error
Is this box pattern syntax really super tied to the placement syntax in the other issues that are also using the box keyword? The resemblance seems cosmetic (also, the tracking issue linked here doesn't mention the pattern syntax except in passing). Boxing recursive enums is a basic thing to do, and being able to deeply match those recursive enums seems like a pure win, regardless of how placement works.
Maybe changing the keyword from box would help get this feature going again?
Reacted by Tesla Zhang, Nestor Demeure, Ross Wolf, ZaneHannanAU, Jason Carr, Robert Balicki, Enderlook, Kai Giebeler, Sava Vranešević, Bubbler and 20 more
As discussed on the RFC for default binding modes in match (tracking issue #42640), I think the best solution here would not be box patterns, but simply to have the ref "default binding mode" automatically propagate through all types with Deref implementations (not just through & itself), and likewise the ref mut mode through all DerefMut types (and not just &mut).
So you could do things like:
let foo = Rc::new(Some(true));// or Box, or Arc, or ...match&foo {Some(b) => {/* b is of type `&bool` here */}None => { ...}}
There may or may not be complications around things like DerefPure here, I can't remember. (If the complications don't implicate safety then I think we should just accept them with, "don't write surprising Deref implementations or you'll get surprising results".)
Reacted by Russell Johnston, Crystal Durham, Jun Wu, Eyal Kalderon, Matthijs Brobbel, Tesla Zhang, Nadrieril, Patrik Buhring, jjpe, jyn and 27 more
Would it also be possible for & in patterns to go through Deref, to preserve the ability to write the "default binding mode" stuff explicitly? Or would it be better to keep box as the explicit version, and let it be stabilized on its own?
Marking Box patterns (in the literal sense of the box keyword) as perma-unstable. If there's forward progress here, it'd be under the auspices of "deref patterns".
It will work for any type, which implements Deref, for example Box, Rc and String.
My library is not drop-in replacement for box patterns, because I call Deref::deref internally, and Deref::deref takes a reference and returns a reference. I. e. with my lib you can do this: match &Box::new(...) { Deref @ _ => ... }, but you cannot do this: match Box::new(...) { Deref @ _ => ... }. I. e. moving a value out of a box is not supported.
Also currently there is no support for DerefMut and RefCell, but they can be trivially added. It is possible that I will add support for them (check periodically my lib on crates.io ). But you can simply download the code and just edit it in few places to get DerefMut and RefCell support
Reacted by Mingwei Samuel
Reacted by Robin Freyler, Tesla Zhang, David Judge, Nadrieril, Kisaragi, Fangsong Long, ninevra, seungha-kim, Mingwei Samuel and Ethan Uppal
@KisaragiEffective , there is already nearly-finished work for strings here: #98914 . My opinion is this: we should create DerefPure as suggested in #98914 (comment) and then make String and Box both impls of DerefPure
Edit: unfortunately, this will not add ability to move out of Box
For anyone hunting for info - this #87121 is the tracking issue for deref patterns, which eliminate the need for box_patterns. So, box_patterns will eventually be deprecated and not stabilized.
Support will probably be added one by one for std types (String, Vec, Box, Rc, Arc) until a trait is decided upon that enables use on custom types like DerefPure (which could only be those where deref has no side effects, per zulip). It seems like it will be a while before we're able to use the result though, since the changes look pretty wide reaching - see the preliminary implementation for String #98914 that merged somewhat recently
Edit: couldn't find one anywhere, so here's a working example with the new string deref syntax:
#![feature(string_deref_patterns)]fnmain(){let s = String::from("this is a string");ifletSome("this is a string") = Some(s){println!("matches!");}}
Reacted by Robin Freyler, Kisaragi, Tesla Zhang, David Judge, rami3l, Ireina, Axel Karjalainen, Yunfei He, Vlad-Shcherbina, Inflation and 31 more
Reacted by Robert, Thalia Archibald and CasperX
Reacted by Arthur Araruna, Andrew Hlynskyi, Amar Sood, Ömer Sinan Ağacan, maxwell8888 and neeko-cat
Activity
pyfisch commentedon Aug 5, 2016
Will it be possible to use box patterns with smart pointers?
clarfonthey commentedon Apr 15, 2017
Odd question: I've seen a few
fn method(self: Box<Self>)type things in libstd; is there any plan to eventually turn this intofn method(box self)?pyfisch commentedon Apr 15, 2017
Types will be written
Box<Foo>and notbox Fooeven if box_patterns is stabilized. Allowingbox Foowould be a new feature that is not planned I think.boxis useful to box values (shorter thenBox::new(value)and allows some pattern matching impossible before.clarfonthey commentedon Apr 15, 2017
Oh, I understand that in general,
box Foowon't be a type. I was just wondering because I was under the assumption that&selfin method definitions was more of a pattern than a type description, which means thatbox selfin methods makes sense.SimonSapin commentedon Apr 15, 2017
&selfin methods does not work like a pattern. For example infn foo(x: &u32) { let &y = x }the&ypattern "removes" one pointer indirection and makeyhave typeu32, whereas in aimpl SomTrait for u32 { fn bar(&self) { … }}methodselfhas type&u32with the&_indirection included.jadbox commentedon Apr 26, 2017
Are 'Box patterns' enabled, without flags, on Nightly yet?
SimonSapin commentedon Apr 26, 2017
@jadbox No. Unless someone missed something, this issue would be closed if they were.
rustc 1.18.0-nightly (2b4c911 2017-04-25)
jadbox commentedon Apr 26, 2017
thanks @SimonSapin
What's left to do on the feature before it can be considered 'stable' enough for nightly?
steveklabnik commentedon Apr 27, 2017
This is related to box syntax more generally, so the questions around that need to be resolved first.
deontologician commentedon Jan 25, 2018
This is really critical to matching recursive enums. I have some code like:
And basically, I'm stuck on nightly.
Is this box pattern syntax really super tied to the placement syntax in the other issues that are also using the
boxkeyword? The resemblance seems cosmetic (also, the tracking issue linked here doesn't mention the pattern syntax except in passing). Boxing recursive enums is a basic thing to do, and being able to deeply match those recursive enums seems like a pure win, regardless of how placement works.Maybe changing the keyword from
boxwould help get this feature going again?glaebhoerl commentedon Jan 25, 2018
As discussed on the RFC for default binding modes in
match(tracking issue #42640), I think the best solution here would not beboxpatterns, but simply to have theref"default binding mode" automatically propagate through all types withDerefimplementations (not just through&itself), and likewise theref mutmode through allDerefMuttypes (and not just&mut).So you could do things like:
There may or may not be complications around things like
DerefPurehere, I can't remember. (If the complications don't implicate safety then I think we should just accept them with, "don't write surprisingDerefimplementations or you'll get surprising results".)rpjohnst commentedon Jan 25, 2018
Would it also be possible for
&in patterns to go throughDeref, to preserve the ability to write the "default binding mode" stuff explicitly? Or would it be better to keepboxas the explicit version, and let it be stabilized on its own?31 remaining items
joshtriplett commentedon Dec 8, 2021
Marking Box patterns (in the literal sense of the
boxkeyword) as perma-unstable. If there's forward progress here, it'd be under the auspices of "deref patterns".safinaskar commentedon Aug 2, 2022
@deontologician, @jonhoo, @jcotton42, @ice1000, @Robbepop, @nirvdrum, @bbqsrc and everybody else interested!
I made proc macro library, which implements deref patterns right now, in stable Rust! 🎉
https://crates.io/crates/match_deref
It will work for any type, which implements Deref, for example Box, Rc and String.
My library is not drop-in replacement for box patterns, because I call Deref::deref internally, and Deref::deref takes a reference and returns a reference. I. e. with my lib you can do this:
match &Box::new(...) { Deref @ _ => ... }, but you cannot do this:match Box::new(...) { Deref @ _ => ... }. I. e. moving a value out of a box is not supported.Also currently there is no support for DerefMut and RefCell, but they can be trivially added. It is possible that I will add support for them (check periodically my lib on crates.io ). But you can simply download the code and just edit it in few places to get DerefMut and RefCell support
KisaragiEffective commentedon Oct 4, 2022
maybe create new gate
min_box_patternand think about only Box on there? Is there blocker to stabilize?safinaskar commentedon Oct 15, 2022
@KisaragiEffective , there is already nearly-finished work for strings here:
#98914 . My opinion is this: we should create
DerefPureas suggested in #98914 (comment) and then make String and Box both impls of DerefPureEdit: unfortunately, this will not add ability to move out of Box
tgross35 commentedon Jan 22, 2023
For anyone hunting for info - this #87121 is the tracking issue for deref patterns, which eliminate the need for
box_patterns. So,box_patternswill eventually be deprecated and not stabilized.Support will probably be added one by one for std types (String, Vec, Box, Rc, Arc) until a trait is decided upon that enables use on custom types like
DerefPure(which could only be those wherederefhas no side effects, per zulip). It seems like it will be a while before we're able to use the result though, since the changes look pretty wide reaching - see the preliminary implementation for String #98914 that merged somewhat recentlyEdit: couldn't find one anywhere, so here's a working example with the new string deref syntax:
Rollup merge of rust-lang#156749 - cyrgani:unbox-full, r=Kivooeo,fmease
Rollup merge of rust-lang#156749 - cyrgani:unbox-full, r=Kivooeo,fmease
Rollup merge of rust-lang#156749 - cyrgani:unbox-full, r=Kivooeo,fmease
Rollup merge of rust-lang#156749 - cyrgani:unbox-full, r=Kivooeo,fmease
Unrolled build for #156749 in rollup 161731
tgross35 commentedon Aug 26, 2026
Feature has been removed in favor of deref patterns, see #156749
Rollup merge of #156749 - cyrgani:unbox-full, r=Kivooeo,fmease