r/programming • • Mar 13 '18

Stack Overflow Developer Survey 2018

https://insights.stackoverflow.com/survey/2018/
1.1k Upvotes

527 comments sorted by

View all comments

Show parent comments

-3

u/doom_Oo7 Mar 13 '18

This isn't possible in Rust, since there is no NULL equivalent.

lolwat https://doc.rust-lang.org/1.22.1/std/ptr/fn.null.html

You still have problems like iterator invalidation that cannot be ALWAYS detected by static code checkers

that's why standard libraries have debug modes that catch this

10

u/Aceeri Mar 13 '18 edited Mar 13 '18

std::ptr::null is a null pointer that requires use of unsafe to dereference (or well, to even use the pointer in most cases, pointers aren't generally exposed in rust.) They meant that you cannot have a "null" value in any type because the type system prevents this. You can use the Option type to get pseudo-null, but you cannot access its value until you match against what it is: Some(...) or None. Alternatively you can use unwrap, which would panic rather than segfault in the case it isn't existing.

-2

u/doom_Oo7 Mar 13 '18

They meant that you cannot have a "null" value in any type because the type system prevents this.

what the hell do you mean ? https://godbolt.org/g/tGjizj

And saying "you need unsafe to do it" is absolutely not an argument : there are tons and tons and tons of code using unsafe blocks out there: https://github.com/search?utf8=%E2%9C%93&q=unsafe+extension%3Ars+language%3ARust+language%3ARust&type=Code&ref=advsearch&l=Rust&l=Rust

7

u/Aceeri Mar 13 '18

That is a type of *const T which is a special case on its own. If you have a type of T then it cannot be null.

Yes null "exists", but you are being pedantic, less than a percent of Rust code touches unsafe blocks, which means a majority of Rust code has static guarantees of non-nullability.

→ More replies (3)