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
//! Collection of some common data structures.
use std::fmt;
/// A point with two dimensions, `x` and `y`.
pub struct Point2D<T> {
/// The first dimension of the point.
pub x: T,
/// The second dimension of the point.
pub y: T
}
impl <T> Point2D<T> {
/// Creates a new point with two dimensions.
pub fn new(x: T, y: T) -> Point2D<T> {
Point2D {
x: x,
y: y
}
}
}
impl <T: fmt::Display + Clone> fmt::Display for Point2D<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({} {})", self.x, self.y)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_point2d() {
let p = Point2D::new(2, 3);
assert_eq!(p.x, 2);
assert_eq!(p.y, 3);
}
}