struct Area
{
width: f64,
height: f64,
is_trig: bool,
}
impl Area
{
fn square(length: f64) -> Self
{
Area{width: length, height: length, is_trig: false}
}
fn cal(&self) -> f64
{
self.width * self.height * if self.is_trig == true {0.5} else {1.}
}
}
fn main()
{
let square = Area::square(10.);
let rect = Area{width: 20., height: 30., is_trig: false};
let trig = Area{width: 20., height: 30., is_trig: true};
println!("Square: {}\nRectangle: {}\nTriangle: {}", square.cal(), rect.cal(), trig.cal());
}
In the code above,
fn cal(&self) -> f64
with this function, how come by default using the self
keyword takes ownership rather than copying the data? Isn't this data stored all on the stack?