22
22
//! - `from` is the more flexible way, which can convert values and references
23
23
//!
24
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.
25
+ //! `Into<U>`, as `From` provides greater flexibility and offer the equivalent `Into`
26
+ //! implementation for free thanks to a blanket implementation in the standard library.
28
27
//!
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>`.
28
+ //! **Note: these traits must not fail**. If the conversion can fail, you must use a dedicated
29
+ //! method which return an `Option<T>` or a `Result<T, E>`.
32
30
//!
33
31
//! # Generic impl
34
32
//!
35
33
//! - `AsRef` and `AsMut` auto-dereference if the inner type is a reference
36
34
//! - `From<U> for T` implies `Into<T> for U`
37
35
//! - `From` and `Into` are reflexive, which means that all types can `into()`
38
- //! themselve and `from()` themselve
36
+ //! themselves and `from()` themselves
39
37
//!
40
38
//! See each trait for usage examples.
41
39
@@ -50,9 +48,8 @@ use marker::Sized;
50
48
///
51
49
/// [book]: ../../book/borrow-and-asref.html
52
50
///
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>`.
51
+ /// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
52
+ /// return an `Option<T>` or a `Result<T, E>`.
56
53
///
57
54
/// # Examples
58
55
///
@@ -73,7 +70,7 @@ use marker::Sized;
73
70
/// # Generic Impls
74
71
///
75
72
/// - `AsRef` auto-dereference if the inner type is a reference or a mutable
76
- /// reference
73
+ /// reference (eg: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
77
74
///
78
75
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
79
76
pub trait AsRef < T : ?Sized > {
@@ -84,14 +81,13 @@ pub trait AsRef<T: ?Sized> {
84
81
85
82
/// A cheap, mutable reference-to-mutable reference conversion.
86
83
///
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>`.
84
+ /// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
85
+ /// return an `Option<T>` or a `Result<T, E>`.
90
86
///
91
87
/// # Generic Impls
92
88
///
93
89
/// - `AsMut` auto-dereference if the inner type is a reference or a mutable
94
- /// reference
90
+ /// reference (eg: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
95
91
///
96
92
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
97
93
pub trait AsMut < T : ?Sized > {
@@ -102,9 +98,12 @@ pub trait AsMut<T: ?Sized> {
102
98
103
99
/// A conversion that consumes `self`, which may or may not be expensive.
104
100
///
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>`.
101
+ /// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
102
+ /// return an `Option<T>` or a `Result<T, E>`.
103
+ ///
104
+ /// Library writer should not implement directly this trait, but should prefer the implementation
105
+ /// of the `From` trait, which offer greater flexibility and provide the equivalent `Into`
106
+ /// implementation for free thanks to a blanket implementation in the standard library.
108
107
///
109
108
/// # Examples
110
109
///
@@ -134,9 +133,8 @@ pub trait Into<T>: Sized {
134
133
135
134
/// Construct `Self` via a conversion.
136
135
///
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>`.
136
+ /// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
137
+ /// return an `Option<T>` or a `Result<T, E>`.
140
138
///
141
139
/// # Examples
142
140
///
0 commit comments