-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add examples for Convert #24121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add examples for Convert #24121
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,35 @@ | |
|
||
//! Traits for conversions between types. | ||
//! | ||
//! The traits in this module provide a general way to talk about | ||
//! conversions from one type to another. They follow the standard | ||
//! Rust conventions of `as`/`to`/`into`/`from`. | ||
//! The traits in this module provide a general way to talk about conversions from one type to | ||
//! another. They follow the standard Rust conventions of `as`/`to`/`into`/`from`. | ||
//! | ||
//! Like many traits, these are often used as bounds for generic functions, to support arguments of | ||
//! multiple types. | ||
//! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add something along the following to the module documentation: While essentially meaning the same thing, both (I'm not completely sure about the factual correctness of the above, but this is the situation as I currently understand it) |
||
//! See each trait for usage examples. | ||
|
||
#![stable(feature = "rust1", since = "1.0.0")] | ||
|
||
use marker::Sized; | ||
|
||
/// A cheap, reference-to-reference conversion. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Both `String` and `&str` implement `AsRef<str>`: | ||
/// | ||
/// ``` | ||
/// fn is_hello<T: AsRef<str>>(s: T) { | ||
/// assert_eq!("hello", s.as_ref()); | ||
/// } | ||
/// | ||
/// let s = "hello"; | ||
/// is_hello(s); | ||
/// | ||
/// let s = "hello".to_string(); | ||
/// is_hello(s); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait AsRef<T: ?Sized> { | ||
/// Performs the conversion. | ||
|
@@ -34,8 +54,21 @@ pub trait AsMut<T: ?Sized> { | |
fn as_mut(&mut self) -> &mut T; | ||
} | ||
|
||
/// A conversion that consumes `self`, which may or may not be | ||
/// expensive. | ||
/// A conversion that consumes `self`, which may or may not be expensive. | ||
/// | ||
/// # Examples | ||
/// | ||
/// `String` implements `Into<Vec<u8>>`: | ||
/// | ||
/// ``` | ||
/// fn is_hello<T: Into<Vec<u8>>>(s: T) { | ||
/// let bytes = b"hello".to_vec(); | ||
/// assert_eq!(bytes, s.into()); | ||
/// } | ||
/// | ||
/// let s = "hello".to_string(); | ||
/// is_hello(s); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait Into<T>: Sized { | ||
/// Performs the conversion. | ||
|
@@ -44,6 +77,19 @@ pub trait Into<T>: Sized { | |
} | ||
|
||
/// Construct `Self` via a conversion. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this line should be grammatically consistent with the first line of |
||
/// | ||
/// # Examples | ||
/// | ||
/// `String` implements `From<&str>`: | ||
/// | ||
/// ``` | ||
/// let s = "hello"; | ||
/// let string = "hello".to_string(); | ||
/// | ||
/// let other_string: String = From::from(s); | ||
/// | ||
/// assert_eq!(string, other_string); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait From<T> { | ||
/// Performs the conversion. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where are these standard conventions documented and what are they?
(I think I know what they are but I'm not really sure)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure, tbh