summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs
blob: 4c5b01bdcdd24069a440e6fab6f95e099eb9c205 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Point<T> {
    x: T,
    y: T,
}

impl<T> Point<T> {
    fn x(&self) -> &T {
        &self.x
    }
}

// ANCHOR: here
impl Point<f32> {
    fn distance_from_origin(&self) -> f32 {
        (self.x.powi(2) + self.y.powi(2)).sqrt()
    }
}
// ANCHOR_END: here

fn main() {
    let p = Point { x: 5, y: 10 };

    println!("p.x = {}", p.x());
}