Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Vector2 {
- x: int,
- y: int
- }
- impl Vector2 {
- pub fn new(xs: int, ys: int) -> Vector2 {
- Vector2 {
- x: xs,
- y: ys
- }
- }
- fn add(&self, otr: Vector2) -> &Vector2 {
- self.x += otr.x; // cannot assign to immutable field
- self.y += otr.y; // cannot assign to immutable field
- return self; // lifetime mismatch
- }
- }
- fn main() {
- let mut vec1 = Vector2::new(42, 12);
- println(fmt!("vec1 = [x: %?, y: %?]", vec1.x, vec1.y));
- let vec2 = Vector2::new(13, 34);
- println(fmt!("vec2 = [x: %?, y: %?]", vec2.x, vec2.y));
- let vec3 = vec1.add(vec2);
- println(fmt!("vec1 + vec2 = [x: %?, y: %?]", vec3.x, vec3.y))
- }
Advertisement
Add Comment
Please, Sign In to add comment