#drop

disown

Drop ownership via a method

1 stable release

Uses new Rust 2021

1.0.0 Mar 9, 2022

#630 in Rust patterns

Download history 24/week @ 2022-06-02 16/week @ 2022-06-09 3/week @ 2022-06-16 10/week @ 2022-06-23 16/week @ 2022-06-30 24/week @ 2022-07-07 13/week @ 2022-07-14 7/week @ 2022-07-21 37/week @ 2022-07-28 2/week @ 2022-08-04 25/week @ 2022-08-11 16/week @ 2022-08-18 50/week @ 2022-08-25 65/week @ 2022-09-01 244/week @ 2022-09-08 108/week @ 2022-09-15

467 downloads per month

MIT license

4KB

disown

Drop ownership from "method position".

Motivation

Normally, unowned data is automatically dropped at the end of its residing block. We can also ignore unuseful return values with ;, which is essentially a T -> () transformation. However, there are cases where we wish to drop ownership and return cleanly with a (), but don't want to involve ; (such as in closures or simple match arms). We could use [std::mem::drop] for this, but drop is a function, not a method, and would visually mar a nice chain of method calls.

Hence the [Disown] trait and its method disown. It is drop, but in "method position".

use disown::Disown;
use std::collections::HashSet;

enum Person {
    Bob,
    Sam,
}

let mut set = HashSet::new();
let person = Person::Bob;

match person {
  Person::Bob => set.insert(0).disown(),
  Person::Sam => set.insert(1).disown(),
}

HashSet::insert returns a bool, not (), and the above code would not compile without opening a pair of {} and using a ;, which doesn't look as nice.

License: MIT

No runtime deps