I'm trying to learn Rust as someone who is somewhat familiar with C++.
I think i grasped the concept of ownership and borrowing pretty well but there is something that confuses me in struct constructors.
struct A{
b: u32
}
impl A{
fn constructor(a:u32)->A{
let obj= A{b:a};
return obj;
}
}
fn main(){
let c= A::constructor(3);
}
In the constructor of A we are allocating an A object in the stack memory of the constructor function and returning the ownership of the object. However, after we return the object, isn't the stack memory of the constructor function going to be de-allocated since it is going to be popped of the stack, leaving us with a dangling pointer c.
I know that passing the ownership outside the the constructor will prevent it from de-allocating when scope of the constructor ends but isn't de-allocation of the stack memory when the function finishes independent from the ownership process?
I would really appreciate any help. I like to continue learning rust with a solid understanding of its memory managment.