]> git.lizzy.rs Git - rust.git/commitdiff
Add doc example to clone trait
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Tue, 22 Mar 2016 00:12:59 +0000 (01:12 +0100)
committerggomez <guillaume1.gomez@gmail.com>
Tue, 22 Mar 2016 17:18:01 +0000 (18:18 +0100)
src/libcore/clone.rs

index b1f63ad71ca3740d1989c7bdfbe96fbb00edb583..a793502e58d371e440d4743c1f54dced7f3d6331 100644 (file)
 //! them cheap and safe to copy. For other types copies must be made
 //! explicitly, by convention implementing the `Clone` trait and calling
 //! the `clone` method.
+//!
+//! Basic usage example:
+//!
+//! ```
+//! let s = String::new(); // String type implements Clone
+//! let copy = s.clone(); // so we can clone it
+//! ```
+//!
+//! To easily implement the Clone trait, you can also use
+//! `#[derive(Clone)]`. Example:
+//!
+//! ```
+//! #[derive(Clone)] // we add the Clone trait to Morpheus struct
+//! struct Morpheus {
+//!    blue_pill: f32,
+//!    red_pill: i64,
+//! }
+//!
+//! fn main() {
+//!    let f = Morpheus { blue_pill: 0.0, red_pill: 0 };
+//!    let copy = f.clone(); // and now we can clone it!
+//! }
+//! ```
 
 #![stable(feature = "rust1", since = "1.0.0")]