mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-11-25 01:01:01 +00:00
48 lines
949 B
Rust
48 lines
949 B
Rust
// ./src/trait/drop.md
|
|
|
|
|
|
struct Droppable {
|
|
name: &'static str,
|
|
}
|
|
|
|
// This trivial implementation of `drop` adds a print to console.
|
|
impl Drop for Droppable {
|
|
fn drop(&mut self) {
|
|
println!("> Dropping {}", self.name);
|
|
}
|
|
}
|
|
|
|
fn part0() {
|
|
let _a = Droppable { name: "a" };
|
|
|
|
// block A
|
|
{
|
|
let _b = Droppable { name: "b" };
|
|
|
|
// block B
|
|
{
|
|
let _c = Droppable { name: "c" };
|
|
let _d = Droppable { name: "d" };
|
|
|
|
println!("Exiting block B");
|
|
}
|
|
println!("Just exited block B");
|
|
|
|
println!("Exiting block A");
|
|
}
|
|
println!("Just exited block A");
|
|
|
|
// Variable can be manually dropped using the `drop` function
|
|
drop(_a);
|
|
// TODO ^ Try commenting this line
|
|
|
|
println!("end of the main function");
|
|
|
|
// `_a` *won't* be `drop`ed again here, because it already has been
|
|
// (manually) `drop`ed
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|