17
17
//! Like many traits, these are often used as bounds for generic functions, to
18
18
//! support arguments of multiple types.
19
19
//!
20
+ //! - Use `as` for reference-to-reference conversions
21
+ //! - Use `into` when you want to consume the value
22
+ //! - `from` is the more flexible way, which can convert values and references
23
+ //!
24
+ //! As a library writer, you should prefer implementing `From<T>` rather than
25
+ //! `Into<U>`, as `From` is more flexible (you can't `Into` a reference, where
26
+ //! you can impl `From` for a reference). `From` is also used for generic
27
+ //! implementations.
28
+ //!
29
+ //! **Note:** these traits are for trivial conversion. **They must not fail**. If
30
+ //! they can fail, use a dedicated method which return an `Option<T>` or
31
+ //! a `Result<T, E>`.
32
+ //!
33
+ //! # Generic impl
34
+ //!
35
+ //! - `AsRef` and `AsMut` auto-dereference if the inner type is a reference
36
+ //! - `From<U> for T` implies `Into<T> for U`
37
+ //! - `From` and `Into` are reflexive, which means that all types can `into()`
38
+ //! themselve and `from()` themselve
39
+ //!
20
40
//! See each trait for usage examples.
21
41
22
42
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -30,6 +50,10 @@ use marker::Sized;
30
50
///
31
51
/// [book]: ../../book/borrow-and-asref.html
32
52
///
53
+ /// **Note:** these traits are for trivial conversion. **They must not fail**. If
54
+ /// they can fail, use a dedicated method which return an `Option<T>` or
55
+ /// a `Result<T, E>`.
56
+ ///
33
57
/// # Examples
34
58
///
35
59
/// Both `String` and `&str` implement `AsRef<str>`:
@@ -45,6 +69,12 @@ use marker::Sized;
45
69
/// let s = "hello".to_string();
46
70
/// is_hello(s);
47
71
/// ```
72
+ ///
73
+ /// # Generic Impls
74
+ ///
75
+ /// - `AsRef` auto-dereference if the inner type is a reference or a mutable
76
+ /// reference
77
+ ///
48
78
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
49
79
pub trait AsRef < T : ?Sized > {
50
80
/// Performs the conversion.
@@ -53,6 +83,16 @@ pub trait AsRef<T: ?Sized> {
53
83
}
54
84
55
85
/// A cheap, mutable reference-to-mutable reference conversion.
86
+ ///
87
+ /// **Note:** these traits are for trivial conversion. **They must not fail**. If
88
+ /// they can fail, use a dedicated method which return an `Option<T>` or
89
+ /// a `Result<T, E>`.
90
+ ///
91
+ /// # Generic Impls
92
+ ///
93
+ /// - `AsMut` auto-dereference if the inner type is a reference or a mutable
94
+ /// reference
95
+ ///
56
96
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
57
97
pub trait AsMut < T : ?Sized > {
58
98
/// Performs the conversion.
@@ -62,6 +102,10 @@ pub trait AsMut<T: ?Sized> {
62
102
63
103
/// A conversion that consumes `self`, which may or may not be expensive.
64
104
///
105
+ /// **Note:** these traits are for trivial conversion. **They must not fail**. If
106
+ /// they can fail, use a dedicated method which return an `Option<T>` or
107
+ /// a `Result<T, E>`.
108
+ ///
65
109
/// # Examples
66
110
///
67
111
/// `String` implements `Into<Vec<u8>>`:
@@ -75,6 +119,12 @@ pub trait AsMut<T: ?Sized> {
75
119
/// let s = "hello".to_string();
76
120
/// is_hello(s);
77
121
/// ```
122
+ ///
123
+ /// #Generic Impls
124
+ ///
125
+ /// - `From<T> for U` implies `Into<U> for T`
126
+ /// - `into()` is reflexive, which means that `Into<T> for T` is implemented
127
+ ///
78
128
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
79
129
pub trait Into < T > : Sized {
80
130
/// Performs the conversion.
@@ -84,6 +134,10 @@ pub trait Into<T>: Sized {
84
134
85
135
/// Construct `Self` via a conversion.
86
136
///
137
+ /// **Note:** these traits are for trivial conversion. **They must not fail**. If
138
+ /// they can fail, use a dedicated method which return an `Option<T>` or
139
+ /// a `Result<T, E>`.
140
+ ///
87
141
/// # Examples
88
142
///
89
143
/// `String` implements `From<&str>`:
@@ -94,6 +148,11 @@ pub trait Into<T>: Sized {
94
148
///
95
149
/// assert_eq!(string, other_string);
96
150
/// ```
151
+ /// # Generic impls
152
+ ///
153
+ /// - `From<T> for U` implies `Into<U> for T`
154
+ /// - `from()` is reflexive, which means that `From<T> for T` is implemented
155
+ ///
97
156
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
98
157
pub trait From < T > : Sized {
99
158
/// Performs the conversion.
0 commit comments