-
Notifications
You must be signed in to change notification settings - Fork 6
/
102_rc_clone.rs
45 lines (37 loc) · 910 Bytes
/
102_rc_clone.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::rc::Rc;
struct Complex {
real: f32,
imag: f32,
}
impl Complex {
fn new(real: f32, imag: f32) -> Complex {
println!("Constructing complex number: {:}+{:}i", real, imag);
Complex{real:real, imag:imag}
}
fn print(&self) {
println!("complex number: {:?}+{:?}i", self.real, self.imag);
}
}
impl Drop for Complex {
fn drop(&mut self) {
println!("Dropping complex number: {:}+{:}i", self.real, self.imag);
}
}
fn main() {
println!("main begin");
let c = Rc::new(Complex::new(0.0, 0.0));
c.print();
{
println!("inner block begin");
let c2 = c.clone();
c2.print();
{
println!("inmost block begin");
let c3 = c.clone();
c3.print();
println!("inmost block end");
}
println!("inner block end");
}
println!("main end");
}