Importantly, the compiler would also have to prove that the assertion has no other side-effects. So yes, the optimizer is always free to remove any code that is guaranteed to have no effect, but that's true whether it's an assertion or not.
Again, this is unlike the C assert macro, which is typically stripped completely in release builds, even if this changes program behavior. The Rust assert! macro has the same semantics in debug and release builds.
assert!() is guaranteed not to be removed to the point where it's often used for ensuring preconditions to unsafe code, even in the standard library. You absolutely can rely on it.
What's the type of somehash? If it's a HashMap, then theinsert takes a key and a value, if it's a HashSet, then it should return just a bool. Assuming a HashMap, surely this is too much, but I feel I would use the entry API:
match somehash.entry(key) {
Entry::VacantEntry(entry) => entry.insert(value),
Entry::OccupiedEntry(_} => panic!("duplicate...")
}
Also I feel I would probably return an error rather than panic in this case, but I don't know your full use case
Usually I would, but here I have some vectors and hashes which have to remain in sync. I'm transferring ownership of a subtree from one tree to another. If the tests passed which allow that to start, it has to finish cleanly or the data structure is broken.