6
6
//! - Implement the [`AsRef`] trait for cheap reference-to-reference conversions
7
7
//! - Implement the [`AsMut`] trait for cheap mutable-to-mutable conversions
8
8
//! - Implement the [`From`] trait for consuming value-to-value conversions
9
- //! - Implement the [`Into`] trait for consuming value-to-value conversions to types outside the current crate
10
- //! - The [`TryFrom`] and [`TryInto`] traits behave like [`From`] and [`Into`], but should be implemented when
9
+ //! - Implement the [`Into`] trait for consuming value-to-value conversions to types
10
+ //! outside the current crate
11
+ //! - The [`TryFrom`] and [`TryInto`] traits behave like [`From`] and [`Into`],
12
+ //! but should be implemented when
11
13
//! the conversion can fail.
12
14
//!
13
15
//! The traits in this module are often used as trait bounds for generic functions such that to
@@ -196,9 +198,10 @@ pub trait AsMut<T: ?Sized> {
196
198
/// A value-to-value conversion that consumes the input value. The
197
199
/// opposite of [`From`].
198
200
///
199
- /// One should only implement [`Into`] if a conversion to a type outside the current crate is required.
200
- /// Otherwise one should always prefer implementing [`From`] over [`Into`] because implementing [`From`] automatically
201
- /// provides one with a implementation of [`Into`] thanks to the blanket implementation in the standard library.
201
+ /// One should only implement [`Into`] if a conversion to a type outside the current crate is
202
+ /// required. Otherwise one should always prefer implementing [`From`] over [`Into`] because
203
+ /// implementing [`From`] automatically provides one with a implementation of [`Into`]
204
+ /// thanks to the blanket implementation in the standard library.
202
205
/// [`From`] cannot do these type of conversions because of Rust's orphaning rules.
203
206
///
204
207
/// **Note: This trait must not fail**. If the conversion can fail, use [`TryInto`].
@@ -209,7 +212,8 @@ pub trait AsMut<T: ?Sized> {
209
212
/// - [`Into`]` is reflexive, which means that `Into<T> for T` is implemented
210
213
///
211
214
/// # Implementing `Into` for conversions to external types
212
- /// If the destination type is not part of the current crate then you can't implement [`From`] directly.
215
+ /// If the destination type is not part of the current crate
216
+ /// then you can't implement [`From`] directly.
213
217
/// For example, take this code:
214
218
///
215
219
/// ```compile_fail
@@ -233,8 +237,9 @@ pub trait AsMut<T: ?Sized> {
233
237
/// }
234
238
/// ```
235
239
///
236
- /// It is important to understand that ```Into``` does not provide a [`From`] implementation (as [`From`] does with ```Into```).
237
- /// Therefore, you should always try to implement [`From`] and then fall back to `Into` if [`From`] can't be implemented.
240
+ /// It is important to understand that ```Into``` does not provide a [`From`] implementation
241
+ /// (as [`From`] does with ```Into```). Therefore, you should always try to implement [`From`]
242
+ /// and then fall back to `Into` if [`From`] can't be implemented.
238
243
/// Prefer using ```Into``` over ```From``` when specifying trait bounds on a generic function
239
244
/// to ensure that types that only implement ```Into``` can be used as well.
240
245
///
@@ -274,8 +279,9 @@ pub trait Into<T>: Sized {
274
279
/// Used to do value-to-value conversions while consuming the input value. It is the reciprocal of
275
280
/// [`Into`].
276
281
///
277
- /// One should always prefer implementing [`From`] over [`Into`] because implementing [`From`] automatically
278
- /// provides one with a implementation of [`Into`] thanks to the blanket implementation in the standard library.
282
+ /// One should always prefer implementing [`From`] over [`Into`]
283
+ /// because implementing [`From`] automatically provides one with a implementation of [`Into`]
284
+ /// thanks to the blanket implementation in the standard library.
279
285
/// Only implement [`Into`] if a conversion to a type outside the current crate is required.
280
286
/// [`From`] cannot do these type of conversions because of Rust's orphaning rules.
281
287
/// See [`Into`] for more details.
@@ -308,11 +314,12 @@ pub trait Into<T>: Sized {
308
314
/// assert_eq!(string, other_string);
309
315
/// ```
310
316
///
311
- /// While performing error handling it is often useful to implement ```From``` for your own error type.
312
- /// By converting underlying error types to our own custom error type that encapsulates the underlying
313
- /// error type, we can return a single error type without losing information on the underlying cause.
314
- /// The '?' operator automatically converts the underlying error type to our custom error type by
315
- /// calling ```Into<CliError>::into``` which is automatically provided when implementing ```From```.
317
+ /// While performing error handling it is often useful to implement ```From```
318
+ /// for your own error type. By converting underlying error types to our own custom error type
319
+ /// that encapsulates the underlying error type, we can return a single error type
320
+ /// without losing information on the underlying cause. The '?' operator automatically converts
321
+ /// the underlying error type to our custom error type by calling ```Into<CliError>::into```
322
+ /// which is automatically provided when implementing ```From```.
316
323
/// The compiler then infers which implementation of ```Into``` should be used.
317
324
///
318
325
/// ```
0 commit comments