rust-lang / rust-lang/rust-by-example
the Drop example isn't good enough
Nobody has claimed this yet.
- Dominant language
- Handlebars
- Stars
- 8.1k
- Forks
- 1.6k
- PR merge metrics
- No merged PRs in 30d
Description
in https://doc.rust-lang.org/rust-by-example/trait/drop.html
there's no example showing how drop() happens in case of inner fields:
the parent struct's drop() gets executed first then the fields' are executed in declaration order(instead of reverse order aka Unlike for structs, local variables are dropped in reverse order) of which they were created.
This seems like an important distinction, even though it's covered in the docs. But not important enough that I figure out how to mod the example via a PR, so this is left for someone far more capable than I :)
This examplifies the above (playground]:
struct Inner {
name: String,
}
impl Inner {
fn new(name: &str) -> Self {
println!("Creating Inner: {}", name);
Inner {
name: name.to_string(),
}
}
}
impl Drop for Inner {
fn drop(&mut self) {
println!("Dropping Inner: {}", self.name);
}
}
struct Parent {
inner1: Inner,
inner2: Inner,
}
impl Parent {
fn new() -> Self {
println!("Creating Parent");
Parent {
inner1: Inner::new("Inner 1i"),
inner2: Inner::new("Inner 2i"),
}
}
fn new2(i1:Inner, i2:Inner) -> Self {
println!("Creating Parent");
Parent {
inner1: i1,
inner2: i2,
}
}
}
impl Drop for Parent {
fn drop(&mut self) {
println!("Dropping Parent");
}
}
fn main() {
{
println!("Example1:");
{
let mut parent = Parent::new();
}
println!("Example2:");
{
//the order of the inners matter when dropping them
let inner1 = Inner::new("New Inner 1");
let inner2 = Inner::new("New Inner 2");
//but it doesn't matter which parent/inners were created first
//in either of the 3 examples in main()
let mut parent = Parent::new2(inner1, inner2);
}
println!("Example3:");
let mut parent = Parent::new();
// Set inner fields to new Inner objects
parent.inner1 = Inner::new("New Inner 1");
parent.inner2 = Inner::new("New Inner 2");
println!("Exiting inner scope");
}
println!("Exiting main scope");
}
output:
Example1:
Creating Parent
Creating Inner: Inner 1i
Creating Inner: Inner 2i
Dropping Parent
Dropping Inner: Inner 1i
Dropping Inner: Inner 2i
Example2:
Creating Inner: New Inner 1
Creating Inner: New Inner 2
Creating Parent
Dropping Parent
Dropping Inner: New Inner 1
Dropping Inner: New Inner 2
Example3:
Creating Parent
Creating Inner: Inner 1i
Creating Inner: Inner 2i
Creating Inner: New Inner 1
Dropping Inner: Inner 1i
Creating Inner: New Inner 2
Dropping Inner: Inner 2i
Exiting inner scope
Dropping Parent
Dropping Inner: New Inner 1
Dropping Inner: New Inner 2
Exiting main scope
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Drop example at https://doc.rust-lang.org/rust-by-example/trait/drop.html and compare it with the linked Rust Playground example. Update the example so it demonstrates parent destruction followed by inner-field destruction in declaration order, then verify the displayed behavior and output against the playground.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100