Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There are probably as many languages using "nil" as there are ones that call it "null".

Nil: Ruby, Go, Oberon, Modula-2, Swift, various Pascal dialects, Lisp?.

Null: C/C++, Java, Rust, JavaScript, Ada.

Then, of course, there's Python with None.



Rust deliberately doesn't have anything in the language called "null" (there's `std::ptr::null()`, but that's for C interop). The only thing that fits the bill here is the unit type/empty tuple, `()`, which is sometimes called "nil".


I know, but it's "std::ptr::null", not "std::ptr::nil", which was the point of this discussion. (Clearly it has a concept of null pointers, even if they're used in a more limited manner than other languages.)


But you can't use `std::ptr::null()` in the same way that you use null in the other languages mentioned there. It's literally just there for C interop, and it's called that because that's the terminology that C uses.

For finding an analogue for the typical null use case in Rust, one would use the `None` variant of the `Option` enum.


Nitpick: `std::ptr::null()` is not just there for C interop. It's also useful when writing pure Rust unsafe code.


Can confirm, used null all the time in Rust. The raw pointers are important native Rust constructs, although they do skew to supporting C idioms as much as possible (e.g. `* const T` and `* mut T` is basically a worthless distinction within Rust itself).


So in other words, std::ptr::null is Rust's version of null for situations where you would actually use null in C, except in general Rust discourages trying to use it like it's C. Seems reasonable.


For what it's worth, Rust has both Null (for unsafe null pointers) and None (wrapping a type that may or may not exist at any given point). None is used far, far more frequently in my experience than null in rust though.


And perl with undef vs (). undef a special symbol such as null, and () the empty list as nil.

Neither nil, null, none or undef refer to (void*)NULL, they are usually a special symbol in the global symbol table. Only some languages use null/nil as NULL/0 internally.


Huh, didn't know it was split that way. I'd seen a couple examples in C/Java/Rust/JS/Java and assumed the vast majority of them used 'null'. Thanks for edifying!


Also..

Nil: Lua

Null: PHP


put SQL on the "null" team as well


Depending on the sql variant, null could be more like nil.


If it varies from NULL then its not SQL compliant https://en.wikipedia.org/wiki/SQL_compliance




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: