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
A-expressionsTerm language related proposals & ideasA-syntaxSyntax related proposals & ideasT-langRelevant to the language team, which will review and decide on the RFC.
Reacted by Blacklight Shining, Andy Hayden, Holger Rapp, oli, Alberto Leal, wimh, JP, remexre, Ian Douglas Scott, Ruben De Smet and 388 more
Reacted by David Souther
Reacted by WuWen, Darrien Glasser, Everton Ribeiro, Artyom Andreev, Brooks Townsend, Felipe S. S. Schneider and Virgiel
Reacted by Denis Kotlyarov, Frederik-Baetens and Safin Singh
Reacted by Campbell Barton, Peter Rudenko, quininer, Artem Borisovskiy, Zverev Konstantin, JoeyAcc, torpak, Joey, Jaanus Varus, Hélio Meira Lins and 63 more
Reacted by Arsen Kondratiev, Julian Bertram, Moritz Hedtke, WuWen, Egeyar Saiu, Darrien Glasser, Everton Ribeiro and Felipe S. S. Schneider
@glaebhoerl, how do you expect this to be done? It seems to me that it would require the ability for patterns to appear in arbitrary positions, which strikes me as completely infeasible.
@bstrie I don't have any plans myself. There was some discussion of this elsewhere, possibly on the rust repository issues - I think the idea might've been that we could take the intersection of the pattern and expression grammars?
Assuming that we took the easy route and made this apply only to assignments, we'd also need to take our grammar from LL(k) to LL(infinity). I also don't think that an arbitrarily restricted pattern grammar will make the language easier to read and understand. Finally, the only time when this feature would be useful is when you can't use a new let binding because of scope, in which case the current workaround is to use a temporary. I'm not currently convinced that the gain is worth the cost.
👍 I've found myself wanting this from time to time, especially in reducing repetition in match statements or normal assignment. Right now I'm using small purpose-built functions instead of this. I haven't considered if it would be possible to abuse a feature like this easily or not.
I would be thrilled if this would be implemented! Here is a small example why:
Currently in libcore/str/mod.rs the function maximal_suffix looks like this:
fnmaximal_suffix(arr:&[u8],reversed:bool) -> (uint,uint){letmut left = -1;// Corresponds to i in the paperletmut right = 0;// Corresponds to j in the paperletmut offset = 1;// Corresponds to k in the paperletmut period = 1;// Corresponds to p in the paperwhile right + offset < arr.len(){let a;let b;if reversed {
a = arr[left + offset];
b = arr[right + offset];}else{
a = arr[right + offset];
b = arr[left + offset];}if a < b {// Suffix is smaller, period is entire prefix so far.
right += offset;
offset = 1;
period = right - left;}elseif a == b {// Advance through repetition of the current period.if offset == period {
right += offset;
offset = 1;}else{
offset += 1;}}else{// Suffix is larger, start over from current location.
left = right;
right += 1;
offset = 1;
period = 1;}}(left + 1, period)}
This could easily look like this:
fnmaximal_suffix(arr:&[u8],reversed:bool) -> (uint,uint){letmut left = -1;// Corresponds to i in the paperletmut right = 0;// Corresponds to j in the paperletmut offset = 1;// Corresponds to k in the paperletmut period = 1;// Corresponds to p in the paperwhile right + offset < arr.len(){let a;let b;if reversed {
a = arr[left + offset];
b = arr[right + offset];}else{
a = arr[right + offset];
b = arr[left + offset];};// Here is the interesting part(left, right, offset, period) =
if a < b {// Suffix is smaller, period is entire prefix so far.(left, right + offset,1, right - left)}elseif a == b {// Advance through repetition of the current period.if offset == period {(left, right + offset,1, period)}else{(left, right, offset + 1, period)}}else{// Suffix is larger, start over from current location.(right, right + 1,1,1)};// end intereseting part}(left + 1, period)}
If we apply, what is currently possible this would be the result:
fnmaximal_suffix(arr:&[u8],reversed:bool) -> (uint,uint){// Corresponds to (i, j, k, p) in the paperlet(mut left,mut right,mut offset,mut period) = (-1,0,1,1);while right + offset < arr.len(){let(a, b) =
if reversed {(arr[left + offset], arr[right + offset])}else{(arr[right + offset], arr[left + offset])};(left, right, offset, period) =
if a < b {// Suffix is smaller, period is entire prefix so far.(left, right + offset,1, right - left)}elseif a == b {// Advance through repetition of the current period.if offset == period {(left, right + offset,1, period)}else{(left, right, offset + 1, period)}}else{// Suffix is larger, start over from current location.(right, right + 1,1,1)};}(left + 1, period)}
This is easily more readble and I guess readbility of code is a major contribution to code safety and attracts more people to the language and projects written in that laguage.
@bombless, a bit but not much. The point of "let" isn't to offer assignment, it's to introduce the variable. Assignment is done with an assignment operator, "=", If we use both the "=" and let for assignment, it becomes redundant. This is why you see:
let mut x: uint;
...
x = 123456789;
the point of this issue is that "let" allows us to unravel tuple-packed variables as we declare them and also set their value in one assignment, rather than multiple assignments; but later throughout the program, the assignment operator ceases to do this unraveling and must be done for each variable.
So there's two ways to do this. With a desugaring pass (easier) or by actually extending the implementation of ExprAssign in the typechecker and translation. The former works, but I suspect it doesn't produce as nice a set of error messages when types don't match.
👍 Ran into this today. I'm surprised that it's not implemented already. A function can return a tuple. If I can bind that tuple via a destructuring let, it's perfectly reasonable also to assign that tuple to some bindings I already have.
Reacted by Campbell Barton, Ruben De Smet, sinesc, amosonn, nightpool, Jason Priest, Rimvydas Naktinis, Artem Borisovskiy, Charlie, Valentyn Valiaiev and 41 more
Reacted by 🏴☠️ 💪📎 , torpak, Rahul Thomas, Julian Bertram, Jan Katzer, Artyom Andreev, Dakota Brink, robinbernon, Virgiel and fritierte
Wrote an initial draft of the RFC here based on this thread.
Comments would be super helpful, thank you!
Reacted by Vlad Frolov, Philipp A., Gábor Lehel, Alexander Regueiro, Karol Woźniak, Matthew Woelk, Iago-lito, Iddan Aaronsohn, Daniel Wyatt, Johannes Harth and 6 more
@fanzier, @cartesiancat and I are going to tackle this feature. We're going to start with an implementation, so we can establish the feasibility, and then follow up with a formal RFC once everything appears to be working.
Reacted by Pavel Korotkov, Valentyn Valiaiev, Karol Woźniak, Gábor Lehel, no, Dmitry Opokin, Ammaar Esmailjee, Vincent Ollivier, Yufan Lou, Cam Cope and 37 more
Reacted by Jake Goulding, Daniel Thul, Vincent Ollivier, Yufan Lou, Brian Rogoff, Bietola, Luo Jia / Zhouqi Jiang, Graham Mitchell, Slanterns, Boscop and 17 more
use std::net::{UdpSocket};fnmain() -> std::io::Result<()>{letmut buf:Vec<u8> = Vec::new();let socket = UdpSocket::bind("0.0.0.0:8080")?;let(_len, src) = socket.recv_from(&mut buf)?;loop{// A resp Packet::Data to B
socket.send_to(&buf, src)?;// B send Packet::Ack to Alet(_len, src) = socket.recv_from(&mut buf)?;}}
There will be a error for: 'src' inside the loop: note: #[warn(unused_variables)] on by default
But if we can destructuring assignment:
use std::net::{UdpSocket,SocketAddr};fnmain() -> std::io::Result<()>{letmut buf:Vec<u8> = Vec::new();let socket = UdpSocket::bind("0.0.0.0:8080")?;let(mut len,mut src):(usize,SocketAddr) = socket.recv_from(&mut buf)?;loop{// A resp Packet::Data to B
socket.send_to(&buf, src)?;// B send Packet::Ack to A(len, src) = socket.recv_from(&mut buf)?;}}
Instead of that as following, something seems verbose:
fnmain() -> std::io::Result<()>{letmut buf:Vec<u8> = Vec::new();let socket = UdpSocket::bind("0.0.0.0:8080")?;let(mut _len,mut src):(usize,SocketAddr) = socket.recv_from(&mut buf)?;loop{// A resp Packet::Data to B
socket.send_to(&buf, src)?;// B send Packet::Ack to Alet(_len, isrc) = socket.recv_from(&mut buf)?;
src = isrc;}}
A-expressionsTerm language related proposals & ideasA-syntaxSyntax related proposals & ideasT-langRelevant to the language team, which will review and decide on the RFC.
Activity
sbditto85 commentedon Dec 30, 2014
Not sure the best way to indicate a vote in the affirmative, but 👍 +1 for this
MattWindsor91 commentedon Dec 30, 2014
Not sure how rust's RFC process goes, but I assume this needs to be written up in the appropriate RFC format first? I like it, mind.
arthurprs commentedon Jan 14, 2015
EDIT: not so fond of it anymore
bstrie commentedon Jan 14, 2015
@glaebhoerl, how do you expect this to be done? It seems to me that it would require the ability for patterns to appear in arbitrary positions, which strikes me as completely infeasible.
glaebhoerl commentedon Jan 14, 2015
@bstrie I don't have any plans myself. There was some discussion of this elsewhere, possibly on the rust repository issues - I think the idea might've been that we could take the intersection of the pattern and expression grammars?
bstrie commentedon Jan 14, 2015
Assuming that we took the easy route and made this apply only to assignments, we'd also need to take our grammar from LL(k) to LL(infinity). I also don't think that an arbitrarily restricted pattern grammar will make the language easier to read and understand. Finally, the only time when this feature would be useful is when you can't use a new
letbinding because of scope, in which case the current workaround is to use a temporary. I'm not currently convinced that the gain is worth the cost.DavidJFelix commentedon Jan 19, 2015
👍 I've found myself wanting this from time to time, especially in reducing repetition in match statements or normal assignment. Right now I'm using small purpose-built functions instead of this. I haven't considered if it would be possible to abuse a feature like this easily or not.
tstorch commentedon Jan 21, 2015
I would be thrilled if this would be implemented! Here is a small example why:
Currently in libcore/str/mod.rs the function
maximal_suffixlooks like this:This could easily look like this:
If we apply, what is currently possible this would be the result:
This is easily more readble and I guess readbility of code is a major contribution to code safety and attracts more people to the language and projects written in that laguage.
bombless commentedon Jan 21, 2015
It doesn't feel right...
If you insist, I think this looks better:
Still doesn't feel right, but looks more reasonable.
DavidJFelix commentedon Jan 21, 2015
So already @bombless this clashes for me as introduce would then become the longest word in rust.
bombless commentedon Jan 21, 2015
@DavidJFelix I don't know, I'd say -1 for this assignment idea.
And maybe change
introducetointrowill make you feel better.DavidJFelix commentedon Jan 21, 2015
@bombless, a bit but not much. The point of "let" isn't to offer assignment, it's to introduce the variable. Assignment is done with an assignment operator, "=", If we use both the "=" and let for assignment, it becomes redundant. This is why you see:
the point of this issue is that "let" allows us to unravel tuple-packed variables as we declare them and also set their value in one assignment, rather than multiple assignments; but later throughout the program, the assignment operator ceases to do this unraveling and must be done for each variable.
taralx commentedon Feb 18, 2015
So there's two ways to do this. With a desugaring pass (easier) or by actually extending the implementation of ExprAssign in the typechecker and translation. The former works, but I suspect it doesn't produce as nice a set of error messages when types don't match.
Thoughts?
carllerche commentedon Mar 9, 2015
I am 👍 for this too
sharpjs commentedon Oct 13, 2015
👍 Ran into this today. I'm surprised that it's not implemented already. A function can return a tuple. If I can bind that tuple via a destructuring
let, it's perfectly reasonable also to assign that tuple to some bindings I already have.92 remaining items
varkor commentedon Sep 25, 2018
If someone wanted to write an RFC taking the points in this thread into account, I'm sure it'd be well-received.
Walther commentedon Feb 27, 2019
Wrote an initial draft of the RFC here based on this thread.
Comments would be super helpful, thank you!
alexreg commentedon Feb 27, 2019
Thanks @Walther, that's super.
Merge pull request rust-lang#372 from runspired/ember-data-custom-rec…
Rollup merge of rust-lang#67538 - varkor:lhs-assign-diagnostics, r=Ce…
Rollup merge of rust-lang#67538 - varkor:lhs-assign-diagnostics, r=Ce…
varkor commentedon Jan 2, 2020
@fanzier, @cartesiancat and I are going to tackle this feature. We're going to start with an implementation, so we can establish the feasibility, and then follow up with a formal RFC once everything appears to be working.
Boscop commentedon Mar 6, 2020
@varkor Thanks, I'm looking forward to it, I often wished this was already possible.
varkor commentedon Apr 17, 2020
@fanzier and I have opened an RFC for destructuring assignment. We also have a working prototype at rust-lang/rust#71156.
m2
efikarl commentedon Dec 4, 2020
I'm looking forward to this feature, too.
For my question:
There will be a error for: 'src' inside the loop: note:
#[warn(unused_variables)]on by defaultBut if we can destructuring assignment:
Instead of that as following, something seems verbose:
Fix reformatting rebase issues