Rust tends towards being explicit rather than implicit. For example, type coercion is done explicitly using as
or into
; and Rust is notable for encouraging functions to declare when they return an error and that the error is handled, even if only to pass it on.
However explicit isn't always the best choice so there are exceptions to this general philosophy. Here's the ones I can think of, are there any more?
-
Deref automatically "dereferences" one type as another type. Should only be used for smart pointers. To be honest I've never been entirely clear on what exactly counts as a "smart pointer" in Rust and what doesn't but
String
(whichderef
s tostr
) is a common and clear example of what definitely is one. - Drop. A function that implicitly runs when a type goes out of scope.
-
Copy. A type marked as copy can be implicitly copied without having to explicitly call a
copy
function. This contrasts with.clone()
. - Type inference can often avoid the need to explicitly write out types within the body of a function but function signatures themselves must be explicit.
- Thread locals are automatically recreated for each new thread without having to explicitly call an init function.
-
Panic. Any function can potentially panic without declaring that it does. Generally speaking this mechanism is used for unrecoverable exceptions but I'd note that what is and isn't recoverable can depend on the context. Using
catch_unwind
may allow for try/catch style handling.