Hacker News new | ask | show | jobs
by andrewjf 1551 days ago
It's trivial to store a null pointer in an Option::Some().

  struct Foo;
  
  fn main() {
      let option_with_null: Option<*const Foo> = Some(std::ptr::null());
  
      dbg!(option_with_null);
      dbg!(option_with_null.is_none());
  }
Output:

  [src/main.rs:6] option_with_null = Some(
      0x0000000000000000,
  )
  [src/main.rs:7] option_with_null.is_none() = false



https://play.rust-lang.org/?version=stable&mode=debug&editio...
1 comments

As the creator of the function, you should try to adhere to the conventions and not try to break them.
I don't disagree, but:

> Some(null) is not a valid result.

It is a valid result. Null pointer and Option::None are still different things, is all.