# Question: closing resources (like std::io::Write)

**URL:** <https://internals.rust-lang.org/t/question-closing-resources-like-std-write/13169>\
**Category:** libs\
**Created:** [October 5, 2020, 9:37pm UTC](https://internals.rust-lang.org/t/question-closing-resources-like-std-write/13169 "2020-10-05T21:37:50Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Mokuz](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mokuz/32/7225_2.png) [@Mokuz](https://internals.rust-lang.org/u/Mokuz)\
**Post date:** [October 6, 2020, 2:28pm UTC](https://internals.rust-lang.org/t/question-closing-resources-like-std-write/13169/6 "2020-10-06T14:28:02Z")

</div>

Just an idea:

```rust
trait TryDrop {
    type Error;
    fn try_drop(&mut self) -> Result<(), Self::Error>;
}

```

and a `T` that implements `TryDrop` can only be dropped in a scope that returns `Result<T, U> where U: From<<T as TryDrop>::Error>`

so code like this:

```rust
impl TryDrop for File {
    /* -snip- */
}

fn main() -> Result<(), Box<dyn Error>> {
    let file = File::open("foo")?;
    // do something with file
    Ok(())
}

```

is converted to code like this

```rust
/* -snip- */

fn main() -> Result<(), Box<dyn Error>> {
    let file = File::open("foo")?;
    // do something with file
    
   file.try_drop()?;
    
    Ok(())
}

```

similarly how `Drop` works (sort of)

---

_[View the full topic](https://internals.rust-lang.org/t/question-closing-resources-like-std-write/13169)._
