Arc<RwLock<HashMap<K, V>>> has performance concerns, the
dashmap crate provides a much better alternative to concurrent hashmaps.
Arc<RwLock<HashMap<K, V>>> is better off replaced by
Arc<DashMap<K, V>> from the
Box::new(T::default()) is overly verbose, involving two function calls instead of one.
It can be better written as
The collections in Rust (
retain(..) method to filter elements matching a given predicate
and discard the rest. The
drain(..) method can be used to do the reverse as well.
Consider using this instead of
Large size differences between enum variants can adversely impact performance,
as the size of the enumeration is bounded by the largest variant.
Having one very large variant can penalize the memory layout of the
These busy loops burn CPU cycles without doing anything. It is almost always a
better idea to
panic! than to have a busy loop.
.bytes().count() is better written as
.len() which is easier to read and more performant.
.bytes().nth(..) is better written as
.as_bytes().get(..) which is more performant.
.split_whitespace() produces an iterator with preceding and trailing
whitespace removed. Thus,
s.trim().split_whitespace() is equivalent to just
collect()on iterable to
Vec has the possible cost of forcing allocation,
hence instead directly using the iterator is more efficient quite often,
Vec is unnecessary.
.replace(..)method calls RS-P1007
replace(..) calls with the same replacement text can result in poor performance. Oftentimes, these
replace calls can be merged.
str functions, such as
.find() work on patterns that
accept both string literals as well as characters. When using such functions,
chars over single-character string literals as they are more
Certain regexes that are used with
RegexBuilder::new may be
replaced by common
str methods, such as
expectfollowed by a function call RS-W1030
expect followed by a function call should be replaced with
unwrap_or_else to increase laziness. The argument to
expect is evaluated
eagerly and unconditionally. The argument to
unwrap_or_else is evaluated
lazily and only if required, sometimes leading to lower overhead and fewer
unwrap_orfollowed by a function call RS-W1031
unwrap_or followed by a function call should be replaced with
unwrap_or_default, to increase laziness. The argument to
unwrap_or is evaluated eagerly and unconditionally.
unwrap_or_default are evaluated lazily and only if required, sometimes
leading to lower overhead and fewer allocations.
.iter().nth(_) is a method of accessing the
nth element of an
iterable. However, some collections such as slices or
VecDeques allow direct
access to the
nth element through helper methods such as
(for mutable access). Not only are these methods more consise, they are also