Why can't the Rust compiler infer the T type parameter in this BTreeMap::range call?
⚓ Rust 📅 2026-04-24 👤 surdeus 👁️ 2When I try to compile this code in Rust Playground using the default settings, I get the following error:
use std::collections::BTreeMap;
use std::ops::Bound::{Included, Excluded};
pub fn get_range(map: &BTreeMap<String, i32>, start: &str, end: &str) -> Vec<(String, i32)> {
map
.range((Included(start), Excluded(end)))
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
}
pub fn main() {
let map = BTreeMap::from_iter(vec![
("cherry".to_string(), 3),
("apple".to_string(), 1),
("banana".to_string(), 2),
]);
let range = get_range(&map, "apple", "cherry");
println!("Range [apple, cherry): {:?}", range);
}
Exited with status 101
Standard Error
Compiling playground v0.0.1 (/playground)
error[E0283]: type annotations needed
--> src/main.rs:6:6
|
6 | .range((Included(start), Excluded(end)))
| ^^^^^ cannot infer type of the type parameter `T` declared on the method `range`
|
= note: cannot satisfy `_: Ord`
note: required by a bound in `BTreeMap::<K, V, A>::range`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/collections/btree/map.rs:1429:12
|
1427 | pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>
| ----- required by a bound in this associated function
1428 | where
1429 | T: Ord,
| ^^^ required by this bound in `BTreeMap::<K, V, A>::range`
help: consider specifying the generic arguments
|
6 | .range::<T, (Bound<&str>, Bound<&str>)>((Included(start), Excluded(end)))
| +++++++++++++++++++++++++++++++++
error[E0283]: type annotations needed
--> src/main.rs:6:6
|
6 | .range((Included(start), Excluded(end)))
| ^^^^^ cannot infer type of the type parameter `T` declared on the method `range`
|
= note: multiple `impl`s satisfying `String: Borrow<_>` found in the following crates: `alloc`, `core`:
- impl Borrow<str> for String;
- impl<T> Borrow<T> for T
where T: ?Sized;
note: required by a bound in `BTreeMap::<K, V, A>::range`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/collections/btree/map.rs:1430:12
|
1427 | pub fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>
| ----- required by a bound in this associated function
...
1430 | K: Borrow<T> + Ord,
| ^^^^^^^^^ required by this bound in `BTreeMap::<K, V, A>::range`
help: consider specifying the generic arguments
|
6 | .range::<T, (Bound<&str>, Bound<&str>)>((Included(start), Excluded(end)))
| +++++++++++++++++++++++++++++++++
For more information about this error, try `rustc --explain E0283`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
When i modify the range call to this: range::<str, _>((Included(start), Excluded(end))). The code runs successfully.
Standard Output
Range [apple, cherry): [("apple", 1), ("banana", 2)]
I do not really understand the compiler reasoning. In my opinion, it should be clear from the trait bounds of the range method that the T parameter can only be a str in this case.
Am I missing something? Would the compiler see it differently? Could someone help me with this?
4 posts - 2 participants
🏷️ Rust_feed