mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 12:43:28 +08:00
23 lines
317 B
Rust
23 lines
317 B
Rust
struct Point<T> {
|
|
x: T,
|
|
y: T,
|
|
}
|
|
|
|
impl<T> Point<T> {
|
|
fn x(&self) -> &T {
|
|
&self.x
|
|
}
|
|
}
|
|
|
|
impl Point<f32, f32> {
|
|
fn distance_from_origin(&self) -> f32 {
|
|
(self.x.powi(2) + self.y.powi(2)).sqrt()
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let p = Point { x: 5, y: 10 };
|
|
|
|
println!("p.x = {}", p.x());
|
|
}
|