November 2017
Intermediate to advanced
264 pages
5h 45m
English
The following code snippet in which the Drop trait is implemented for a struct clearly shows the moment when the value block is freed:
// see code in Chapter 7/code/drop.rs
struct Block {
number: i32
}
impl Drop for Block {
fn drop(&mut self) {
println!("Dropping!");
}
}
fn print_block(block: Block) {
println!("In function print_block");
}
fn main() {
let block = Block{ number: 1 };
// move of value block:
print_block(block);
println!("Back in main!");
}
This prints out the following output:
In function print_blockDropping!Back in main!
So the block reference is freed at the end of the print_block function.
Read now
Unlock full access