Skip to content

Instantly share code, notes, and snippets.

@eventhelix
Last active May 14, 2022 21:13
Show Gist options
  • Save eventhelix/2cf16135c352f4e97ff4ac0c8ac0c5d2 to your computer and use it in GitHub Desktop.
Save eventhelix/2cf16135c352f4e97ff4ac0c8ac0c5d2 to your computer and use it in GitHub Desktop.
Complex numbers
use std::rc::Rc;
use std::sync::Arc;
pub struct Complex {
real: f64,
imaginary: f64,
}
impl Complex {
pub fn magnitude_self_ownership(self) -> f64 {
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt()
}
pub fn magnitude_self_reference(&self) -> f64 {
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt()
}
// Passing smart pointers
pub fn magnitude_self_box(self: Box<Self>) -> f64 {
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt()
}
pub fn magnitude_self_rc(self: Rc<Self>) -> f64 {
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt()
}
pub fn magnitude_self_arc(self: Arc<Self>) -> f64 {
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment