@@ -249,37 +249,58 @@ mod prim_pointer { }
249
249
#[ doc( primitive = "array" ) ]
250
250
//
251
251
/// A fixed-size array, denoted `[T; N]`, for the element type, `T`, and the
252
- /// non-negative compile time constant size, `N`.
252
+ /// non-negative compile- time constant size, `N`.
253
253
///
254
- /// Arrays values are created either with an explicit expression that lists
255
- /// each element: `[x, y, z]` or a repeat expression: `[x; N]`. The repeat
256
- /// expression requires that the element type is `Copy`.
254
+ /// There are two syntactic forms for creating an array:
257
255
///
258
- /// The type `[T; N]` is `Copy` if `T: Copy`.
256
+ /// * A list with each element, i.e. `[x, y, z]`.
257
+ /// * A repeat expression `[x; N]`, which produces an array with `N` copies of `x`.
258
+ /// The type of `x` must be [`Copy`][copy].
259
259
///
260
260
/// Arrays of sizes from 0 to 32 (inclusive) implement the following traits if
261
261
/// the element type allows it:
262
262
///
263
- /// - `Clone` (only if `T: Copy`)
264
- /// - `Debug`
265
- /// - `IntoIterator` (implemented for `&[T; N]` and `&mut [T; N]`)
266
- /// - `PartialEq`, `PartialOrd`, `Ord`, `Eq`
267
- /// - `Hash`
268
- /// - `AsRef`, `AsMut`
269
- /// - `Borrow`, `BorrowMut`
270
- /// - `Default`
271
- ///
272
- /// This limitation to `N in 0..33 ` exists because Rust does not yet support
273
- /// generics over the size of an array type. `[Foo; 3]` and `[Bar; 3]` are
274
- /// instances of same generic type `[T; 3]`, but `[Foo; 3]` and `[Foo; 5]` are
263
+ /// - [ `Clone`][clone] (only if `T: Copy`)
264
+ /// - [ `Debug`][debug]
265
+ /// - [ `IntoIterator`][intoiterator] (implemented for `&[T; N]` and `&mut [T; N]`)
266
+ /// - [ `PartialEq`][partialeq], [ `PartialOrd`][partialord], [`Eq`][eq], [`Ord`][ord]
267
+ /// - [ `Hash`][hash]
268
+ /// - [ `AsRef`][asref], [ `AsMut`][asmut]
269
+ /// - [ `Borrow`][borrow], [ `BorrowMut`][borrowmut]
270
+ /// - [ `Default`][default]
271
+ ///
272
+ /// This limitation on the size `N ` exists because Rust does not yet support
273
+ /// code that is generic over the size of an array type. `[Foo; 3]` and `[Bar; 3]`
274
+ /// are instances of same generic type `[T; 3]`, but `[Foo; 3]` and `[Foo; 5]` are
275
275
/// entirely different types. As a stopgap, trait implementations are
276
- /// statically generated for `N in 0..33` .
276
+ /// statically generated up to size 32 .
277
277
///
278
- /// Arrays coerce to [slices (`[T]`)][slice], so their methods can be called on
279
- /// arrays. Slices are dynamic and do not coerce to arrays; consequently more
280
- /// methods are defined on `slice` where they support both types.
278
+ /// Arrays of *any* size are [`Copy`][copy] if the element type is `Copy`. This
279
+ /// works because the `Copy` trait is specially known to the compiler.
280
+ ///
281
+ /// Arrays coerce to [slices (`[T]`)][slice], so a slice method may be called on
282
+ /// an array. Indeed, this provides most of the API for working with arrays.
283
+ /// Slices have a dynamic size and do not coerce to arrays.
284
+ ///
285
+ /// There is no way to move elements out of an array. See [`mem::replace`][replace]
286
+ /// for an alternative.
281
287
///
282
288
/// [slice]: primitive.slice.html
289
+ /// [copy]: marker/trait.Copy.html
290
+ /// [clone]: clone/trait.Clone.html
291
+ /// [debug]: fmt/trait.Debug.html
292
+ /// [intoiterator]: iter/trait.IntoIterator.html
293
+ /// [partialeq]: cmp/trait.PartialEq.html
294
+ /// [partialord]: cmp/trait.PartialOrd.html
295
+ /// [eq]: cmp/trait.Eq.html
296
+ /// [ord]: cmp/trait.Ord.html
297
+ /// [hash]: hash/trait.Hash.html
298
+ /// [asref]: convert/trait.AsRef.html
299
+ /// [asmut]: convert/trait.AsMut.html
300
+ /// [borrow]: borrow/trait.Borrow.html
301
+ /// [borrowmut]: borrow/trait.BorrowMut.html
302
+ /// [default]: default/trait.Default.html
303
+ /// [replace]: mem/fn.replace.html
283
304
///
284
305
/// # Examples
285
306
///
@@ -295,7 +316,30 @@ mod prim_pointer { }
295
316
/// for x in &array {
296
317
/// print!("{} ", x);
297
318
/// }
319
+ /// ```
320
+ ///
321
+ /// An array itself is not iterable:
322
+ ///
323
+ /// ```ignore
324
+ /// let array: [i32; 3] = [0; 3];
325
+ ///
326
+ /// for x in array { }
327
+ /// // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied
328
+ /// ```
298
329
///
330
+ /// The solution is to coerce the array to a slice by calling a slice method:
331
+ ///
332
+ /// ```
333
+ /// # let array: [i32; 3] = [0; 3];
334
+ /// for x in array.iter() { }
335
+ /// ```
336
+ ///
337
+ /// If the array has 32 or fewer elements (see above), you can also use the
338
+ /// array reference's `IntoIterator` implementation:
339
+ ///
340
+ /// ```
341
+ /// # let array: [i32; 3] = [0; 3];
342
+ /// for x in &array { }
299
343
/// ```
300
344
///
301
345
mod prim_array { }
0 commit comments