Reasons for Dot Notation for Tuple

后端 未结 4 2033
挽巷
挽巷 2021-01-11 09:20

Is there any technical reason Rust is designed to use dot notation for tuples instead of using index notation (t[2])?

let t = (20u32, true, \'b\         


        
4条回答
  •  不思量自难忘°
    2021-01-11 10:10

    This decision was made in RFC 184. The Motivation section has details:

    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.

    The discussion in the associated pull request is also useful.

提交回复
热议问题