4 ms·
"unsafe" is expressing that you are opting-in for less safe code. Same in C#. Many unsafe abstractions are not opt-in in C++. You can go ahead and writing them
by partycoder 10y ago
"unsafe" is expressing that you are opting-in for less safe code. Same in C#.
Many unsafe abstractions are not opt-in in C++. You can go ahead and writing them. No warning or cost.
"mut" and "unsafe" are keywords that you need to write down each time. They're keywords you can spot easily during a code review and say "how do you justify this?".
Rust's language design choice of having everything being immutable and safe by default is a great idea.
- Manishearth 10y ago> "unsafe" is expressing that you are opting-in for less safe code. Same in C#. Yes, but that is scoped. You can write unsafe code when designing an abstraction, and seal off the unsafety with a safe-to-use API. You then verify the abstraction, and are free to use it however you want. This is what's usually done, and this is what unsafe is for. "mut" isn't really a red-flag keyword. Sure, you don't want unnecessary mutation and everything is immutable by default, but nobody really has issues with making things mutable. Rust is not a language where purity is important. "unsafe" is, but for designing abstractions that's what unsafe is for, and while you still should justify it, "I need an abstraction like this and it can't be implemented in safe Rust because <reasons>" should be enough. Of course, you should be able to explain why it is safe to use, preferably in the docs/comments.
- partycoder 10y agoNo but "mut" comes with a maintenance cost. Where it is harder to make assumptions over its value.