mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 20:14:45 +00:00
1.2 KiB
1.2 KiB
Guidelines for Rust code
Local rules
- Use
log!
for debugging output (useprintln!
etc. for output processing required for release). - Unused or internal variables and methods (private and used only for specific functions) should be prefixed with
_
. To avoid conflicts with reserved words, add one trailing_
.
Encouraged code
- Define domain-specific Enums instead of numeric enumerations or bools.
- Minimize access modifiers. Use
pub(mod)
orpub(crate)
in preference even when publishing. - Explicitly convert iterable objects in for expressions to iterators (
for i in x.iter()
instead offor i in x
). - Evaluate Lazily. For example, use
unwrap_or_else
instead ofunwrap_or
ifdefault
is not a literal.
Unsolicited code
-
Use return-type overloading a lot. Specifically, code that uses non-trivial
.into
too often. This is because the result of type inference may be counter-intuitive. In this case, it is recommended to usefrom
instead. -
Codes that use
Deref
a lot. This causes practically the same problem as inheritance.
Code that changes its decision depending on the context
- Define unused helper methods.
- Uses
unwrap
,clone
. In some cases, there is no choice but to do so.