November 2018
Intermediate to advanced
390 pages
10h 8m
English
Variables are moved to new locations, preventing the previous location from using them. We need to define the owner of the variable, so we can't just create a new variable randomly and use it.
In the following code, Box is the allocation operation. We are passing it to the helper function. As we can see in the following example, in the first helper function call with the first slot value, the ownership is moved under the helper function. During the second call, it fails, because the second call has the Box<int> type, which can't be copied:
fn main() { let slot = Box 1; helper(slot); // moves the value helper(slot); // error: use of moved value }fn helper(slot: Box<int>) { println!("The Number was: {}", slot) ...Read now
Unlock full access