Q:

Unwrap a BTreeSet in rust

In rust, the following function is legal:
fn unwrap<T>(s:Option<T>) -> T {
    s.unwrap()
}

It takes ownership of s, panics if it is a None, and returns ownership of the contents of s (which is legal since an Option owns its contents).
I was trying to write a similar function with signature
fn unwrap_set<T>(s: BTreeSet<T>) -> T { 
  ...
}

The idea is that it panics unless s has size 1, in which case it returns the single element. It seems like this should be possible for the same reason unwrap is possible, however none of the methods on BTreeSet had the right signature (they would need to have return type T). The closest was take, and I tried to do
    let mut s2 = s;
    let v: &T = s2.iter().next().unwrap();
    s2.take(v).unwrap()

but this failed.
Is writing a function like unwrap_set possible?

A:

The easiest way to do this would be to use BTreeSet<T>'s implementation of IntoIterator, which would allow you to easily pull owned values out of the set one at a time:
fn unwrap_set<T>(s: BTreeSet<T>) -> T {
    let mut it = s.into_iter();
    if let Some(first) = it.next() {
        if let None = it.next() {
            return first;
        }
    }
    panic!("set must have a single value");
}

If you wanted to indirectly rely on IntoIterator you could also use a normal loop, but I don't think it's as readable that way so I probably wouldn't do this:
fn unwrap_set<T>(s: BTreeSet<T>) -> T {
    let mut result = None;
    for item in s {
        // If there is a second value, bail out
        if let Some(_) = result {
            result = None;
            break;
        }
        result = Some(item);
    }
    return result.expect("set must have a single value");
}

