- Start Date: 2014-10-15
- RFC PR: rust-lang/rfcs#344
- Rust Issue: rust-lang/rust#18074
Summary
This is a conventions RFC for settling a number of remaining naming conventions:
- Referring参照するto types in method names
- Iterator type names
- Additional追加のiterator method names
- Getter/setter APIs
- Associated関連付けられたtypes
- Trait naming
- Lint naming
- Suffix ordering順序
- Prelude traits
It also proposes to standardize on lower
Motivation
As part of the ongoing API stabilization process, we need to settle naming conventions for public APIs. This RFC is a continuation of that process, addressing a number of smaller but still global naming issues.
Detailed design設計(する)
The RFC includes a number of unrelated naming conventions, broken down into subsections below.
Referring参照する to types in method names
Function names often involve type names, the most common example being conversionsas_slice
. If the type has a purely textual name (ignoring parameters), it is straightforward to convert
Type name | Text in methods |
---|---|
String | string |
Vec<T> | vec |
YourType | your_type |
Types that involve notation
Type name | Text in methods |
---|---|
&str | str |
&[T] | slice |
&mut [T] | mut_slice |
&[u8] | bytes |
&T | ref |
&mut T | mut |
*const T | ptr |
*mut T | mut_ptr |
The only surprise here is the use of mut
rather than mut_ref
for mutable references.as_ref
and as_mut
methods), and is meant to keep this very common case short.
Iterator type names
The current convention for iterator type names is the following:
Iterators require introducing and exporting new types. These types should use the following
下記の、次に続く、追従するnaming convention:
Base
基となる、基底(の)name. If the iterator yields産出する、出力するsomething that can be described記述するwith a specific特定のnoun, the base基となる、基底(の)name should be the pluralization of that noun (e.g. an iterator yielding産出する、出力するwords is called呼び出しWords
). Generic contains含むuse the base基となる、基底(の)nameItems
.Flavor prefix
接頭辞. Iterators often come in multiple複数のflavors, with the default flavor providing与えるimmutable不変のreferences.参照Other flavors should prefix接頭辞their name:
- Moving iterators have a prefix
接頭辞ofMove
.- If the default iterator yields
産出する、出力するan immutable不変のreference,参照an iterator yielding産出する、出力するa mutable reference参照has a prefix接頭辞Mut
.- Reverse iterators have a prefix
接頭辞ofRev
.
(These conventions were established as part of this PR and later this one.)
These conventions have not yet been updated to reflect the recent change to the iterator method names, in part to allow
-
They are fairly loose and therefore not mechanical or predictable. In particular, the choice of noun to use for the base
基となる、基底(の)name is completely arbitrary.任意の -
They are not always applicable. The
iter
module, for example, defines定義するa large number of iterator types for use in the adapter methods onIterator
(e.g.Map
formap
,Filter
forfilter
, etc.) The module does not follow下記の、次に続く、追従するthe convention, and it's not clear how it could do so.
This RFC proposes to instead aligniter
module: the name of an iterator type should be the same as the method that produces
For example:
iter
would yield産出する、出力するanIter
iter_mut
would yield産出する、出力するanIterMut
into_iter
would yield産出する、出力するanIntoIter
These type names make the most sense when prefixed with their owning module, e.g. vec::IntoIter
.
Advantages:
-
The rule is completely mechanical, and therefore highly predictable.
-
The convention can be (almost) universally followed:
下記の、次に続く、追従するit applies適用するequally well tovec
and toiter
.
Disadvantages:
-
IntoIter
is not an ideal name. Note, however, that since we've moved tointo_iter
as the method name, the existing convention (MoveItems
) needs to be updated to match,一致する、マッチさせるand it's not clear how to do better thanIntoItems
in any case. -
This naming scheme can result
結果、戻り値in clashes if multiple複数のcontainers are defined定義するin the same module. Note that this is already the case with today's conventions. In most cases, this situation should be taken as an indication that a more refined module hierarchy階層is called呼び出しfor.
Additional追加の iterator method names
An earlier RFC settled the conventions for the "standard" iterator methods: iter
, iter_mut
, into_iter
.
However, there are many cases where you also want "nonstandard" iterator methods: bytes
and chars
for strings, keys
and values
for maps, the various
This RFC proposes the following
-
Use
iter
(and variants) for data types that can be viewed as containers, and where the iterator provides与えるthe "obvious" sequence連なり、並びof contained items. -
If there is no single
単一の"obvious" sequence連なり、並びof contained items, or if there are multiple複数のdesired views on the container, provide与えるseparate methods for these that do not useiter
in their name. The name should instead directly直接reflect the view/item type being iterated繰り返す、反復する(likebytes
). -
Likewise, for iterator adapters (
filter
,map
and so on) or other iterator-producing operations演算、操作(intersection
), use the clearest name to describe the adapter/operation directly,直接and do not mentioniter
. -
If not otherwise
さもなければqualified,修飾するan iterator-producing method should provide与えるan iterator over immutable不変のreferences.参照Use the_mut
suffix for variants producing産出、産出するmutable references,参照and theinto_
prefix接頭辞for variants consuming the data in order順序to produce産出するowned values.
Getter/setter APIs
Some data structures do not wish to provide
The proposed convention for a field foo: T
is:
- A method
foo(&self) -> &T
for getting the current value of the field. - A method
set_foo(&self, val: T)
for settingセットする、集合the field. (Theval
argument引数here may takeとる&T
or some other type, depending on the context.)
Note that this convention is about getters/setters on ordinary data types, not on builder objects. The naming conventions for builder methods are still open.
Associated関連付けられた types
Unlike type parameters,
AssociatedErr
rather than E
, and Item
rather than T
.
Trait naming
The wiki guidelines have long suggested naming traits as follows:
Prefer (transitive) verbs, nouns, and then adjectives; avoid
避ける、回避するgrammatical suffixes (likeable
)
Trait names like Copy
, Clone
and Show
follow
This RFC proposes to amend the convention to furtherClone
and ToCStr
, for example.
AccordingEncodable
should probably be Encode
.
There are some open questions about these rules; see Unresolved Questions below.
Lints
Our lint names are not consistent. While this may seem like a minor concern, when we hit 1.0 the lint names will be locked down, so it's worth trying to clean them up now.
The basic rule is: the lint name should make sense when read as "allowdeprecated
items" and "allowdead_code
" makes sense, while "allowunsafe_block
" is ungrammatical (should be plural).
Specifically,
-
Lint names should state the bad thing being checked for, e.g.
deprecated
, so that#[allow(deprecated)]
(items) reads correctly. Thusそれゆえに、従って、ctypes
is not an appropriate name;improper_ctypes
is. -
Lints that apply
適用するto arbitrary任意のitems (like the stability lints) should just mention what they check for: usedeprecated
rather thandeprecated_items
. This keeps lint names short. (Again, think "allow許可する、可能にするlint-name items".) -
If a lint applies
適用するto a specific特定のgrammatical class,分類mention that class分類and use the plural form:形式、形態、形作るuseunused_variables
rather thanunused_variable
. This makes#[allow(unused_variables)]
read correctly. -
Lints that catch unnecessary, unused, or useless aspects of code should use the term
項、用語unused
, e.g.unused_imports
,unused_typecasts
. -
Use snake case in the same way you would for function names.
Suffix ordering順序
Very occasionally, conventions will require a method to have multipleget_unchecked_mut
. When feasible, design
Because it is so rare,
However, the mut suffix is so common, and is now entrenched as showing up in final position, that this RFC does propose one simple rule: if there are multiplemut
, place mut
last.
Prelude traits
It is not currently possible to definechar
or slices. Consequently,libcore
and other basic crates provideImmutableSlice
or Char
) that are intended to be implemented
These traits are generally not designed
This RFC proposes to use a Prelude
suffix for these basic traits. Since the traits are, in fact, included in the prelude their names do not generally appear
(There is one important drawback in today's Rust: associated
Error messages
Error messages -- including those producedfail!
and those placed in the desc
or detail
fields of e.g. IoError
-- should in generalrustc
and std
.
This is already the predominant convention, but there are some inconsistencies.
Alternatives代わりのもの、選択肢
Iterator type names
The iterator type name convention could instead basically stick with today's convention, but using suffixes instead of prefixes,IntoItems
rather than MoveItems
.
Unresolved questions
How far should the rules for trait names go? Should we avoidRead
rather than Reader
?