Skip to content

Commit 7a38ac8

Browse files
committed
Rollup merge of rust-lang#32416 - GuillaumeGomez:patch-3, r=steveklabnik
Add doc example to clone trait Fixes rust-lang#29346. r? @steveklabnik
2 parents 87aee45 + b3aa35e commit 7a38ac8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libcore/clone.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@
1818
//! them cheap and safe to copy. For other types copies must be made
1919
//! explicitly, by convention implementing the `Clone` trait and calling
2020
//! the `clone` method.
21+
//!
22+
//! Basic usage example:
23+
//!
24+
//! ```
25+
//! let s = String::new(); // String type implements Clone
26+
//! let copy = s.clone(); // so we can clone it
27+
//! ```
28+
//!
29+
//! To easily implement the Clone trait, you can also use
30+
//! `#[derive(Clone)]`. Example:
31+
//!
32+
//! ```
33+
//! #[derive(Clone)] // we add the Clone trait to Morpheus struct
34+
//! struct Morpheus {
35+
//! blue_pill: f32,
36+
//! red_pill: i64,
37+
//! }
38+
//!
39+
//! fn main() {
40+
//! let f = Morpheus { blue_pill: 0.0, red_pill: 0 };
41+
//! let copy = f.clone(); // and now we can clone it!
42+
//! }
43+
//! ```
2144
2245
#![stable(feature = "rust1", since = "1.0.0")]
2346

0 commit comments

Comments
 (0)