// Library template
// Public API - these functions can be used by other crates
pub fn hello(name: string) -> string {
"Hello, ${name}!"
}
pub fn add(a: int, b: int) -> int {
a + b
}
pub fn multiply(a: int, b: int) -> int {
a * b
}
// Example struct
@derive(Debug, Clone, PartialEq)
pub struct Point {
pub x: float,
pub y: float
}
impl Point {
pub fn new(x: float, y: float) -> Point {
Point { x: x, y: y }
}
pub fn distance(self, other: Point) -> float {
let dx = self.x - other.x
let dy = self.y - other.y
(dx * dx + dy * dy).sqrt()
}
}
// Tests
@test
fn test_hello() {
let result = hello("World")
assert_eq!(result, "Hello, World!")
}
@test
fn test_add() {
assert_eq!(add(2, 3), 5)
assert_eq!(add(-1, 1), 0)
}
@test
fn test_multiply() {
assert_eq!(multiply(3, 4), 12)
assert_eq!(multiply(0, 100), 0)
}
@test
fn test_point_distance() {
let p1 = Point::new(0.0, 0.0)
let p2 = Point::new(3.0, 4.0)
let dist = p1.distance(p2)
assert_eq!(dist, 5.0)
}