diff options
| author | Dylan Maccora <maccora17@gmail.com> | 2017-04-04 08:27:20 +1000 |
|---|---|---|
| committer | Dylan Maccora <maccora17@gmail.com> | 2017-04-04 08:27:20 +1000 |
| commit | bb8474682366592fa9d52cb11723a5fd5cd9421e (patch) | |
| tree | 3bc8ff51a70bc09630adb9f33d8701362a60e26e /src/libcore | |
| parent | 79efca10934b3ea0a172db0ab514fdd72ddeacfb (diff) | |
| download | rust-bb8474682366592fa9d52cb11723a5fd5cd9421e.tar.gz rust-bb8474682366592fa9d52cb11723a5fd5cd9421e.zip | |
AsMut example
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/convert.rs | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 19cd2d3398f..0bc05506413 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -91,6 +91,8 @@ use str::FromStr; /// [`Path`]: ../../std/path/struct.Path.html /// /// ``` +/// use std::path::Path; +/// /// impl AsRef<Path> for str { /// fn as_ref(&self) -> &Path { /// Path::new(self) @@ -123,7 +125,8 @@ pub trait AsRef<T: ?Sized> { /// A cheap, mutable reference-to-mutable reference conversion. /// -/// This trait is similar to `AsRef` but used for converting mutable references. +/// This trait is similar to `AsRef` but used for converting between mutable +/// references. /// /// **Note: this trait must not fail**. If the conversion can fail, use a /// dedicated method which returns an [`Option<T>`] or a [`Result<T, E>`]. @@ -153,11 +156,13 @@ pub trait AsRef<T: ?Sized> { /// assert_eq!(*boxed_num, 1); /// ``` /// -/// Implementing `AsMut`: +/// `Vec` implements `AsMut` for converting between itself and a primitive array: /// /// ``` -/// impl Type { -/// let a = 1; +/// impl<T> AsMut<[T]> for Vec<T> { +/// fn as_mut(&mut self) -> &mut [T] { +/// self +/// } /// } /// ``` /// @@ -250,19 +255,22 @@ pub trait Into<T>: Sized { /// An example usage for error handling: /// /// ``` +/// use std::io::{self, Read}; +/// use std::num; +/// /// enum CliError { /// IoError(io::Error), /// ParseError(num::ParseIntError), /// } /// -/// impl From<io::Error> for MyError { +/// impl From<io::Error> for CliError { /// fn from(error: io::Error) -> Self { /// CliError::IoError(error) /// } /// } /// -/// impl From<num::ParseIntError> for MyError { -/// fn from(error: io::Error) -> Self { +/// impl From<num::ParseIntError> for CliError { +/// fn from(error: num::ParseIntError) -> Self { /// CliError::ParseError(error) /// } /// } |
