From 762f367f6d602e135537c2e85502e27803242a8f Mon Sep 17 00:00:00 2001 From: Nik Revenco Date: Fri, 25 Jul 2025 13:11:54 +0100 Subject: [PATCH] Methods named `len`, `is_empty` and `capacity` should take constant time --- src/predictability.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/predictability.md b/src/predictability.md index a044f0a..b9de799 100644 --- a/src/predictability.md +++ b/src/predictability.md @@ -131,6 +131,26 @@ so on for the other traits. [`std::ops`]: https://doc.rust-lang.org/std/ops/index.html#traits + + +## Methods `len`, `is_empty` and `capacity` take constant time (C-CONSTANT-TIME-METHODS) + +The [time complexity] for methods named `len`, `is_empty` and `capacity` should be **O(1)** - +it should always take the same amount of time to run these methods regardless of +how large the collection is. + +If you need to count elements by iterating, choose a different name for your methods such as `count` or `size` + +### Examples from the standard library + +- [`Vec::len`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.len) +- [`Vec::is_empty`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.is_empty) +- [`Vec::capacity`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.capacity) +- [`HashMap::len`](https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.len) +- [`HashSet::capacity`](https://doc.rust-lang.org/std/collections/struct.HashSet.html#method.capacity) +- [`LinkedList::is_empty`](https://doc.rust-lang.org/std/collections/struct.LinkedList.html#method.is_empty) + +[time complexity]: https://en.wikipedia.org/wiki/Time_complexity ## Only smart pointers implement `Deref` and `DerefMut` (C-DEREF)