reference

Does setting variable to null clear just the reference? [duplicate]

∥☆過路亽.° 提交于 2020-11-28 06:26:35
问题 This question already has answers here : What is the difference between a variable, object, and reference? [duplicate] (5 answers) Closed 5 years ago . What happens if I do: Object obj = new Object(); obj = null; Does it remove the object from memory, or clear just the reference? More formally, consider the code: Object obj = new Object(); Object temp = obj; obj = null; Why is temp still not null ? Shouldn't it have been removed from memory? 回答1: In your first example: obj -------> |OBJECT|

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

北慕城南 提交于 2020-11-26 13:28:31
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

蹲街弑〆低调 提交于 2020-11-26 13:21:52
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

天涯浪子 提交于 2020-11-26 13:20:40
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state: