|
|
@ -0,0 +1,26 @@
|
|
|
1
|
// adopted from the lifetime guide: http://doc.rust-lang.org/guide-lifetimes.html
|
|
|
2
|
|
|
|
3
|
#[deriving(Show)]
|
|
|
4
|
struct Point {
|
|
|
5
|
x: f64,
|
|
|
6
|
y: f64
|
|
|
7
|
}
|
|
|
8
|
|
|
|
9
|
fn main() {
|
|
|
10
|
let on_the_stack : Point = Point{x: 3.4, y: 5.7};
|
|
|
11
|
let on_the_heap : Box<Point> = box Point{x: 9.6, y: 2.1};
|
|
|
12
|
|
|
|
13
|
let dist = compute_distance(&on_the_stack, &*on_the_heap);
|
|
|
14
|
|
|
|
15
|
// mismatched types: &Point vs Box<Point>
|
|
|
16
|
// e.g., there is no automatic "promoting" of boxed to reference pointers
|
|
|
17
|
//let dist2 = compute_distance(&on_the_stack, on_the_heap);
|
|
|
18
|
|
|
|
19
|
println!("dist({}, {}) = {}", on_the_stack, on_the_heap, dist)
|
|
|
20
|
}
|
|
|
21
|
|
|
|
22
|
fn compute_distance(p1: &Point, p2: &Point) -> f64 {
|
|
|
23
|
let dx = p1.x - p2.x;
|
|
|
24
|
let dy = p1.y - p2.y;
|
|
|
25
|
(dx*dx + dy*dy).sqrt()
|
|
|
26
|
}
|