Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 24, 2020 12:23
Show Gist options
  • Save rust-play/34c919617ad4fdbef287171a9f775d97 to your computer and use it in GitHub Desktop.
Save rust-play/34c919617ad4fdbef287171a9f775d97 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// 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