- Start Date: 2014-07-24
- RFC PR #: https://github.com/rust-lang/rfcs/pull/184
- Rust Issue #: https://github.com/rust-lang/rust/issues/16950
Add simple syntax文法
for accessing values within tuplesタプル(複合型)
and tupleタプル(複合型)
structs構造、構造体
behind a feature gate.
Right now accessing fields of tuplesタプル(複合型)
and tupleタプル(複合型)
structs構造、構造体
is incredibly painful—one must rely on pattern-matching alone to extract抽出する
values. This became such a problem that twelve traits were created in the standard library (core::tuple::Tuple*
) to make tupleタプル(複合型)
value accesses easier, addingたす
.valN()
, .refN()
, and .mutN()
methods to help this. But this is not a very nice solution—it requires the traits to be implemented実装する
in the standard library, not the language,言語
and for those traits to be imported on use. On the whole this is not a problem, because most of the time std::prelude::*
is imported, but this is still a hack which is not a real solution to the problem at hand. It also only supports tuplesタプル(複合型)
of length up to twelve, which is normally not a problem but emphasises how bad the current situation is.
Add syntax文法
of the form形式、形態、形作る
<expr>.<integer>
for accessing values within tuplesタプル(複合型)
and tupleタプル(複合型)
structs.構造、構造体
This (and the functionality it provides) would only be allowed許可する、可能にする
when the feature gate tuple_indexing
is enabled. This syntax文法
is recognised wherever an unsuffixed integer整数
literal is found in place of the normal field or method name expected when accessing fields with .
. Because the parser would be expecting an integer,整数
not a float, an expression式
like expr.0.1
would be a syntax文法
error (because 0.1
would be treated取り扱う
as a single単一の
token).
Tuple/tuple struct構造、構造体
field access behaves振る舞う
the same way as accessing named fields on normal structs:構造、構造体
#![allow(unused)]
fn main() {
struct Foo(int, int);
let mut foo = Foo(3, -15);
foo.0 = 5;
assert_eq!(foo.0, 5);
struct Foo2 { _0: int, _1: int }
let mut foo2 = Foo2 { _0: 3, _1: -15 };
foo2._0 = 5;
assert_eq!(foo2._0, 5);
}
Effectively, a tupleタプル(複合型)
or tupleタプル(複合型)
struct構造、構造体
field is just a normal named field with an integer整数
for a name.
This adds more complexity that is not strictly necessary.
Stay with the status quo. Either recommend using a struct構造、構造体
with named fields or suggest using pattern-matching to extract抽出する
values. If extracting抽出する
individual個々の、それぞれの
fields of tuplesタプル(複合型)
is really necessary, the TupleN
traits could be used instead, and something like #[deriving(Tuple3)]
could possiblyことによると、可能性としてあり得ることに
be addedたす
for tupleタプル(複合型)
structs.構造、構造体
None.