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

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
宝玉的分享
宝玉的分享
量子位
V
Visual Studio Blog
罗磊的独立博客
Vercel News
Vercel News
B
Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
GbyAI
GbyAI
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

The Rust Programming Language Forum - Latest posts

What's everyone working on this week (21/2026)? Rust Function Call with one Param Iced + Canvas + Text: How do I create a style for canvas text "Value dropped while borrowed" as a lifetime mismatch Weird `use of moved value` behaviour Slightly surprising behavior of a while loop Clap: how to disable options after a certain positional argument How do i tell rust-analyzer what kind of bracket to use for a macro? Iterator + Borrow Checker: Work around borrowing already borrowed mutable variable Requesting data via mavlink SKILLS.md for Rust development 🧵 Stringlet UTF-8 Hack Option Niche? Iterator + Map - How do divide every element, including the last element, by the last element Why can't we store returned value of a function in a variable if one of the parameters goes out of scope My first crate :D, floop: A more convenient and less error prone replacement for loop `{ select! { .. }}` Sorting is slow because architectures are wrong — zan-sort redesigns the architecture, not the algorithm Iced Font - Create a custom monospace font to display dollar amounts Is the UnsafePinned RFC wrong about being able to return `&mut T` from `get_mut_unchecked`? Fixing Polars 0.37 compilation errors: Hashbrown 0.17 dependency conflict and decimal parsing Any plans for improving error diagnostic in Rust 2.0? How do I build completely offline? Is `&mut T -> &mut ManuallyDrop<T>` well-defined and sound? Would you use this? — fixtura, declarative fake data injection for tests Foreign trait restrictions on native types make generics hard to use Cargo Exclude Directive Compiler reasoning around a modulo counter Multiple mutable references to elements within one vector Handling non-`Send` data in a `Send` closure Review: Static Multi Pool Allocator Rmquickjs - High-level MicroQuickJS bindings for Rust
Why can&#39;t the Rust compiler infer the T type paramete...
@mmarcell264 · 2026-04-25 · via The Rust Programming Language Forum - Latest posts
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?