惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
博客园 - 【当耐特】
博客园 - 叶小钗
量子位
博客园 - 聂微东
S
SegmentFault 最新的问题
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
宝玉的分享
宝玉的分享
小众软件
小众软件
罗磊的独立博客
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog

The Rust Programming Language Forum - Latest topics

C++ to Rust -- Exceptions Re-exporting a trait with a custom derive_macro Gold linker is deprecated Why is using PhantomData valid in this case? Code review for Static Pool Allocator Looking for a pool based allocator Why is this legal? What does it mean for Rustc to run "out of TLS keys"? Using async/await internally with no internal runtime, but exposing a nonblocking poll API — is this a reasonable design? Testing functions that use randomness `cargo-path`: improve coding agents&#39; ability to find Rust documentation Rust task runners Lifetime weird case Windows - USB device not detected A random rustc-ice-[...].txt file appeared Develop rust where the environment is setup in a docker Undefined Behavior: in-bounds pointer arithmetic failed: attempting to offset pointer by 20 bytes, but got alloc238 which is only 1 byte from the end of the allocation Unbug 0.5 - Runtime debug assertions Is there a tiny error in section 6.2 Reference types? Lifetime woes implementing ratatui::Widget for a reference Rusqlite + Chrono: How do I simplify code to obtain chrono datetime value From OOP to Rust – struggling with code organization and data structure design Way to avoid a self-referential struct Rust RF and audio resources/communities Whyhttp - HTTP mocks that fail where the bug actually is Using tokio channel permits in a tower service Arc::increment_strong_count design question (cross-post) `&T`, `&mut T`, `Pin<&mut T>` and `&Cell<T>`: Ways of Borrowing a `T` Ratatui detect arrow key press and release Feedback about post in medium
Why can&#39;t the Rust compiler infer the T type paramete...
@mmarcell264 · 2026-04-25 · via The Rust Programming Language Forum - Latest topics
When 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?