Created
January 24, 2020 12:23
-
-
Save rust-play/34c919617ad4fdbef287171a9f775d97 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An example to illustrate that 'mut' in front of a parameter name | |
// is NOT part of the signature of the function. | |
pub struct Point { | |
x: f64, | |
y: f64, | |
} | |
pub trait Translate: Sized { | |
fn translate(&mut self, dx: f64, dy: f64); | |
fn translated(mut self, dx: f64, dy: f64) -> Self { | |
// ^^^ | |
// `mut` is here because it is required by the default implementation, | |
// but it is not part of the signature, as illustrated by Point's impl | |
// line 27 below | |
self.translate(dx, dy); | |
self | |
} | |
} | |
impl Translate for Point { | |
fn translate(&mut self, dx: f64, dy: f64) { | |
self.x += dx; | |
self.y += dy; | |
} | |
fn translated(self, dx: f64, dy: f64) -> Self { | |
// ^^^ | |
// no `mut` here; this implementation does not need it | |
Point { | |
x: self.x+dx, | |
y: self.y+dy, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment