mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 12:14:43 +00:00
1.3 KiB
1.3 KiB
Guidelines for Rust code
local rules
- Use
log!
for output for debugging (useprintln!
etc. for output processing that is also necessary for release). - Unused or internal variables/methods (private and used only for specific functions) must be prefixed with
_
. If you want to avoid conflicts with reserved words, add one_
to the end.
Recommended code
- Define and use domain-specific Enums instead of numeric enumerations or bools.
- Keep access modifiers to a minimum. Prioritize using
pub(mod)
orpub(crate)
even when publishing. - Convert an iterable object in a for expression explicitly to an iterator (
for i in x.iter()
instead offor i in x
). - Lazy evaluation. For example, if
default
is non-literal, useunwrap_or_else
instead ofunwrap_or
.
Code not encouraged
- Make heavy use of return type overloading. Specifically code that uses a lot of non-obvious
.into
. This is because type inference results can be counter-intuitive. In this case it is recommended to usefrom
instead. - Make heavy use of
Deref
. This effectively poses the same problem as inheritance.
Code that makes decisions based on context
- Define unused helper methods.
- Use
unwrap
andclone
a lot. In some cases there is nothing better than doing so.