pub fn set_or_check<T>(maybe: &mut Option<T>, value: T)
where
    T: std::fmt::Debug + PartialEq
{
    let previous = maybe.replace(value);
    if previous.is_some() {
        assert!(&previous == maybe, "Expected {:?} and {:?} to be equal", &maybe, &previous);
    }
}

pub fn set_or_check<T>(maybe: &mut Option<T>, value: T)
where
    T: std::fmt::Debug + PartialEq
{
    let previous = maybe.replace(value);

    assert!(&previous == maybe, "Expected {:?} and {:?} to be equal", &maybe, &previous);
}

--- 
+++ 
@@ -3,6 +3,7 @@
     T: std::fmt::Debug + PartialEq
 {
     let previous = maybe.replace(value);
-
-    assert!(&previous == maybe, "Expected {:?} and {:?} to be equal", &maybe, &previous);
+    if previous.is_some() {
+        assert!(&previous == maybe, "Expected {:?} and {:?} to be equal", &maybe, &previous);
+    }
 }
Handle setting the first value

No need to check the value the first time it is replaced.
