-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtraits_20191122_1532_1.rs
More file actions
64 lines (54 loc) · 963 Bytes
/
traits_20191122_1532_1.rs
File metadata and controls
64 lines (54 loc) · 963 Bytes
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
struct Vector2
{
x: f64,
y: f64
}
struct Vector3
{
x: f64,
y: f64,
z: f64
}
trait Modulus
{
fn modulus(&self) -> f64;
}
trait ToStr
{
fn to_str(&self) -> String;
}
impl Modulus for Vector2
{
fn modulus(&self) -> f64
{
return (self.x*self.x + self.y*self.y).sqrt();
}
}
impl Modulus for Vector3
{
fn modulus(&self) -> f64
{
return (self.x*self.x + self.y*self.y + self.z*self.z).sqrt();
}
}
impl ToStr for Vector2
{
fn to_str(&self) -> String
{
return format!("V2(x={:.2}, y={:.2})", self.x, self.y);
}
}
impl ToStr for Vector3
{
fn to_str(&self) -> String
{
return format!("V3(x={:.2}, y={:.2}, z={:.2})", self.x, self.y, self.z);
}
}
fn main() {
let a = Vector2{x: 10.0, y: 11.0};
let b = Vector3{x: 31.0, y: 32.0, z: 33.0};
println!("A={} \nB={}", a.to_str(), b.to_str());
println!("A.modulus={} \nB.modulus={}", a.modulus(), b.modulus());
println!("Hello World!");
}