-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tuple issue #41081
Comments
You can also add a space:
I think the tuple access syntax is one of the few design mistakes of Rust. A array-indexing-like syntax like in D/Python language avoids that problem altogether:
And it can be used in static for loops as in D language:
But perhaps now it's too much late to fix the tuple indexing syntax even in epoch. |
This bug in particular was an incorrect span being used for the suggestion and a unittest not actually checking the output. Thanks for the bug report! |
For some history here: https://github.com/rust-lang/rfcs/blob/master/text/0184-tuple-accessors.md |
Thank you for the link. Unfortunately the drawbacks of the t.0.0 syntax was ignored in that RFC, the "Unresolved questions" is empty, and the "Alternatives" lacks a discussion of the [] syntax. In the implementation thread they say:
This sounds silly, and ignores both the D language and Python, and the possible usage in static for loops as in D language... So it's a Rust wart for me. |
FWIW I think the tuple syntax in general is fine, but the error with nested indexing that's highlighted here is one of those annoying "I know what you meant, but I'll make you rephrase it anyway" errors that we could consider simply removing in the name of usability. |
You can't use it to index statically. |
Rust is a statically-typed language, so you can't index tuples dynamically anyway. |
That's the point of a static loop, take a look at the D code above. There's no dynamic tuple indexing in it. |
I don't read D fluently, but that D code seems to have a tuple with three items of the same type, which is weird -- in Rust you'd just use an array. Actually, in Rust you'd use a macro to get the same effect, right? Like this: macro_rules! tuploop {
(foreach |$tup:expr, $i:ident: [$($idx:tt),*]| $code:expr) => {
$(
let $i = $tup.$idx;
$code;
)*
}
}
fn main() {
let tup = (1, "hi");
tuploop! {
foreach |tup, i: [0, 1]| {
println!("{}", i);
}
}
} |
That D code isn't weird, it's just an example. If you want an example tuple with different types here it is:
Thank you for your macro. I think the current Rust tuple indexing syntax isn't good enough, it wasn't discussed enough during the language design phase. Probably it's too much late now to fix it even in a Rust Epoch. |
Use proper span for tuple index parsed as float Fix diagnostic suggestion from: ```rust help: try parenthesizing the first index | (1, (2, 3)).((1, (2, 3)).1).1; ``` to the correct: ```rust help: try parenthesizing the first index | ((1, (2, 3)).1).1; ``` Fix rust-lang#41081.
I defined a tuple:
let tuple: ((u8, u32),(u8, u32)) = ((1, 45), (2, 100));
And I have a tool to look up its type:
So I can get the type of the tuple:
type_of(tuple.0);
and it turns out to be(u8, u32)
.Right.According to convention, I can use
tuple.0.0
to get the firstu8
value. Becausetype_of(tuple.0)
is(u8, u32)
. But when I usetuple.0
, I get:A little weird of this wrong message... It tells us Rust recognized it as
tuple.(0.0)
...And I do as the "help"'s advice,
type_of(tuple.(tuple.0).3)
and then get another wrong msg...But I use
(tuple.0).0
is okay...If it's not made deliberately, hope you can fix this syntax instead of
(tuple.0).0
and make the help message better~;)Besides,
The text was updated successfully, but these errors were encountered: