Skip to content

Commit bb84746

Browse files
committed
AsMut example
1 parent 79efca1 commit bb84746

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/libcore/convert.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ use str::FromStr;
9191
/// [`Path`]: ../../std/path/struct.Path.html
9292
///
9393
/// ```
94+
/// use std::path::Path;
95+
///
9496
/// impl AsRef<Path> for str {
9597
/// fn as_ref(&self) -> &Path {
9698
/// Path::new(self)
@@ -123,7 +125,8 @@ pub trait AsRef<T: ?Sized> {
123125

124126
/// A cheap, mutable reference-to-mutable reference conversion.
125127
///
126-
/// This trait is similar to `AsRef` but used for converting mutable references.
128+
/// This trait is similar to `AsRef` but used for converting between mutable
129+
/// references.
127130
///
128131
/// **Note: this trait must not fail**. If the conversion can fail, use a
129132
/// dedicated method which returns an [`Option<T>`] or a [`Result<T, E>`].
@@ -153,11 +156,13 @@ pub trait AsRef<T: ?Sized> {
153156
/// assert_eq!(*boxed_num, 1);
154157
/// ```
155158
///
156-
/// Implementing `AsMut`:
159+
/// `Vec` implements `AsMut` for converting between itself and a primitive array:
157160
///
158161
/// ```
159-
/// impl Type {
160-
/// let a = 1;
162+
/// impl<T> AsMut<[T]> for Vec<T> {
163+
/// fn as_mut(&mut self) -> &mut [T] {
164+
/// self
165+
/// }
161166
/// }
162167
/// ```
163168
///
@@ -250,19 +255,22 @@ pub trait Into<T>: Sized {
250255
/// An example usage for error handling:
251256
///
252257
/// ```
258+
/// use std::io::{self, Read};
259+
/// use std::num;
260+
///
253261
/// enum CliError {
254262
/// IoError(io::Error),
255263
/// ParseError(num::ParseIntError),
256264
/// }
257265
///
258-
/// impl From<io::Error> for MyError {
266+
/// impl From<io::Error> for CliError {
259267
/// fn from(error: io::Error) -> Self {
260268
/// CliError::IoError(error)
261269
/// }
262270
/// }
263271
///
264-
/// impl From<num::ParseIntError> for MyError {
265-
/// fn from(error: io::Error) -> Self {
272+
/// impl From<num::ParseIntError> for CliError {
273+
/// fn from(error: num::ParseIntError) -> Self {
266274
/// CliError::ParseError(error)
267275
/// }
268276
/// }

0 commit comments

Comments
 (0)