I have the following code:
struct SomeStruct {
foo: i32
}
impl SomeStruct {
fn new() -> SomeStruct {
SomeStruct { foo: 42 }
}
}
fn main() {
let val = SomeStruct::new();
}
When one calls SomeStruct::new()
how many copy operations are there? First, returning value was created on the stack of the new
function. Is it copied right after it to the stack of the main
function and the original one is destroyed due to leaving the scope (C++ behaviour)?
And a more general use case:
struct SomeResource {
handle: i32 // For example, it's open file descriptor.
}
impl SomeResource {
fn new() -> SomeResource {
SomeResource { handle: 0 }
}
}
struct SomeClient {
r: SomeResource
}
impl SomeClient {
fn new() -> SomeClient {
SomeClient { r: SomeResource::new() }
}
}
fn main() {
SomeClient::new();
}
Is this a common way to create nested objects in Rust?