@@ -106,6 +106,18 @@ semantics.
106
106
[ `GzDecoder` ] : https://docs.rs/flate2/0.2.19/flate2/read/struct.GzDecoder.html#method.into_inner
107
107
[ `AtomicBool` ] : https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html#method.into_inner
108
108
109
+ If the ` mut ` qualifier in the name of a conversion method constitutes part of
110
+ the return type, it should appear as it would appear in the type. For example
111
+ [ ` Vec::as_mut_slice ` ] returns a mut slice; it does what it says. This name is
112
+ preferred over ` as_slice_mut ` .
113
+
114
+ [ `Vec::as_mut_slice` ] : https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_mut_slice
115
+
116
+ ``` rust
117
+ // Return type is a mut slice.
118
+ fn as_mut_slice (& mut self ) -> & mut [T ];
119
+ ```
120
+
109
121
##### More examples from the standard library
110
122
111
123
- [ ` Result::as_ref ` ] ( https://doc.rust-lang.org/std/result/enum.Result.html#method.as_ref )
@@ -191,65 +203,45 @@ example [`vec::IntoIter`].
191
203
[btree_map :: Values ]: https : // doc.rust-lang.org/std/collections/btree_map/struct.Values.html
192
204
193
205
194
- <a id = " c-own-suffix" ></ a >
195
- ## Ownership suffixes use `_mut `, `_ref ` (C - OWN - SUFFIX )
196
-
197
- Functions often come in multiple variants : immutably borrowed , mutably borrowed ,
198
- and owned .
199
-
200
- The right default depends on the function in question . Variants should be marked
201
- through suffixes .
202
-
203
- ### Exceptions
204
-
205
- In the case of iterators , the moving variant should be understood as an `into `
206
- conversion , `into_iter `.
207
-
208
- For mutably borrowed variants , if the `mut ` qualifier is part of a type name ,
209
- it should appear as it would appear in the type . For example
210
- [`Vec :: as_mut_slice `] returns a mut slice ; it does what it says .
211
-
212
- [`Vec :: as_mut_slice `]: https : // doc.rust-lang.org/std/vec/struct.Vec.html#method.as_mut_slice
213
-
214
- ### Immutably borrowed by default
215
-
216
- If `foo ` uses / produces an immutable borrow by default , use a `_mut` suffix (e. g.
217
- `foo_mut`) for the mutably borrowed variant.
218
-
219
- #### Examples from the standard library
220
-
221
- TODO [api- guidelines#37](https: // github.com/rust-lang-nursery/api-guidelines/issues/37)
222
-
223
- ### Owned by default
224
-
225
- If `foo` uses/ produces owned data by default, use :
226
-
227
- * The `_ref` suffix (e. g. `foo_ref`) for the immutably borrowed variant.
228
- * The `_mut` suffix (e. g. `foo_mut`) for the mutably borrowed variant.
206
+ <a id = " c-getter" ></ a >
207
+ ## Getter names follow Rust convention (C - GETTER )
229
208
230
- #### Examples from the standard library
231
-
232
- - [`std :: io :: BufReader :: get_ref`](https: // doc.rust-lang.org/std/io/struct.BufReader.html#method.get_ref)
233
- - [`std :: io :: BufReader :: get_mut`](https: // doc.rust-lang.org/std/io/struct.BufReader.html#method.get_mut)
234
-
235
-
236
- <a id= "c- getters"></ a>
237
- ## Single - element containers implement appropriate getters (C - GETTERS )
238
-
239
- Single - element containers where accessing the element cannot fail should
240
- implement `get` and `get_mut`, with the following signatures.
209
+ With a few exceptions , the `get_ ` prefix is not used for getters in Rust code .
241
210
242
211
```rust
243
- fn get(& self ) -> & V ;
244
- fn get_mut (& mut self ) -> & mut V ;
212
+ pub struct S {
213
+ first : First ,
214
+ second : Second ,
215
+ }
216
+
217
+ impl S {
218
+ // Not get_first.
219
+ pub fn first (& self ) -> & First {
220
+ & self . first
221
+ }
222
+
223
+ // Not get_first_mut, get_mut_first, or mut_first.
224
+ pub fn first_mut (& mut self ) -> & mut First {
225
+ & mut self . first
226
+ }
227
+ }
245
228
```
246
229
247
- For getters that do runtime validation, consider adding unsafe ` _unchecked `
248
- variants.
230
+ The ` get ` naming is used only when there is a single and obvious thing that
231
+ could reasonably be gotten by a getter. For example [ ` Cell::get ` ] accesses the
232
+ content of a ` Cell ` .
233
+
234
+ [ `Cell::get` ] : https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get
235
+
236
+ For getters that do runtime validation such as bounds checking, consider adding
237
+ unsafe ` _unchecked ` variants. Typically those will have the following
238
+ signatures.
249
239
250
240
``` rust
251
- unsafe fn get_unchecked (& self , index ) -> & V ;
252
- unsafe fn get_unchecked_mut (& mut self , index ) -> & mut V ;
241
+ fn get (& self , index : K ) -> Option <& V >;
242
+ fn get_mut (& mut self , index : K ) -> Option <& mut V >;
243
+ unsafe fn get_unchecked (& self , index : K ) -> & V ;
244
+ unsafe fn get_unchecked_mut (& mut self , index : K ) -> & mut V ;
253
245
```
254
246
255
247
### Examples from the standard library
@@ -259,7 +251,7 @@ unsafe fn get_unchecked_mut(&mut self, index) -> &mut V;
259
251
- [ ` std::sync::PoisonError::get_mut ` ] ( https://doc.rust-lang.org/std/sync/struct.PoisonError.html#method.get_mut )
260
252
- [ ` std::sync::atomic::AtomicBool::get_mut ` ] ( https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html#method.get_mut )
261
253
- [ ` std::collections::hash_map::OccupiedEntry::get_mut ` ] ( https://doc.rust-lang.org/std/collections/hash_map/struct.OccupiedEntry.html#method.get_mut )
262
- - [ ` <[_ ]>::get_unchecked ` ] ( https://doc.rust-lang.org/std/primitive.slice.html#method.get_unchecked )
254
+ - [ ` <[T ]>::get_unchecked ` ] ( https://doc.rust-lang.org/std/primitive.slice.html#method.get_unchecked )
263
255
264
256
265
257
<a id =" c-feature " ></a >
0 commit comments