I'm a bit of a Rust newbie, but some of the "good" things seem like semantics. Maybe you can correct my thinking here?
There are no exceptions
The compiler throws a ton of exceptions, Rust is famous for it. And there's panic!, which in my experience is more common than advertised.
There is no null
But there is None, which doesn't seem that different to me. Is there a difference in the way you handle a nullable value in another language (say int?) and the way you handle optional values in Rust (like Option<isize>)?
I agree with your other points. Rust is my favorite language for performance-sensitive scripts, and I'm always happy to see things being rewritten in it.
What I wanted to say is that there are no exceptions in the same sense there are in other languages (like Java, C++ or C# for example). panic! exists but is used for non-recoverable errors (and while technically it is possible to catch a panic with catch_unwind it's not guaranteed you'll be able to do it). Basically, try / catch mechanism has been replaced by introducing error handling by Result.
Returning Result allows you to have information about functions that could fail ingrained into the type system, forcing you to act on it. But I understand that this could lead to confusion for beginners so I'll edit my comment.
None and null are not the same thing. Option<T> type is in fact an enum which could only be Some<T> or None, therefore everything that could be done with other enums could be done with Option<T> - you can match on it and there are several built-in functions like and_then or unwrap_or for more granular control.
null on the other hand basically means null pointer or a pointer that does not point to any valid object. While modern languages like Kotlin do introduce optional types, they won't prevent you from having uninitialized variables, because you need to use String? instead of String to ensure you don't end up having null. In Rust, this is prevented by ensuring every variable must be initialized before it's used. Whether you have an Optional<i32> or i32, you need to initialize it first.
So, while they may seem as the same thing, null and None are not the same.
Keep in mind that things I wrote are focused on the safe Rust. Unsafe Rust, on the other hand, is a different beast and topic for itself; there is for example std::men::MaybeUninit for wrapping possibly uninitialized instances.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm a bit of a Rust newbie, but some of the "good" things seem like semantics. Maybe you can correct my thinking here?
The compiler throws a ton of exceptions, Rust is famous for it. And there's
panic!, which in my experience is more common than advertised.But there is
None, which doesn't seem that different to me. Is there a difference in the way you handle a nullable value in another language (sayint?) and the way you handle optional values in Rust (likeOption<isize>)?I agree with your other points. Rust is my favorite language for performance-sensitive scripts, and I'm always happy to see things being rewritten in it.
What I wanted to say is that there are no exceptions in the same sense there are in other languages (like Java, C++ or C# for example).
panic!exists but is used for non-recoverable errors (and while technically it is possible to catch a panic with catch_unwind it's not guaranteed you'll be able to do it). Basically, try / catch mechanism has been replaced by introducing error handling byResult.Returning
Resultallows you to have information about functions that could fail ingrained into the type system, forcing you to act on it. But I understand that this could lead to confusion for beginners so I'll edit my comment.Noneandnullare not the same thing.Option<T>type is in fact an enum which could only beSome<T>orNone, therefore everything that could be done with other enums could be done withOption<T>- you can match on it and there are several built-in functions likeand_thenorunwrap_orfor more granular control.nullon the other hand basically means null pointer or a pointer that does not point to any valid object. While modern languages like Kotlin do introduce optional types, they won't prevent you from having uninitialized variables, because you need to useString?instead ofStringto ensure you don't end up havingnull. In Rust, this is prevented by ensuring every variable must be initialized before it's used. Whether you have anOptional<i32>ori32, you need to initialize it first.So, while they may seem as the same thing,
nullandNoneare not the same.Keep in mind that things I wrote are focused on the safe Rust. Unsafe Rust, on the other hand, is a different beast and topic for itself; there is for example
std::men::MaybeUninitfor wrapping possibly uninitialized instances.