• 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

Summary

Add simple syntax

文法
for accessing values within tuples
タプル(複合型)
and tuple
タプル(複合型)
structs
構造、構造体
behind a feature gate.

Motivation

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.

Detailed design
設計(する)

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() { // With tuple struct struct Foo(int, int); let mut foo = Foo(3, -15); foo.0 = 5; assert_eq!(foo.0, 5); // With normal struct 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.

Drawbacks

This adds more complexity that is not strictly necessary.

Alternatives
代わりのもの、選択肢

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.
構造、構造体

Unresolved questions

None.