Closed
Description
Given the following code (playground):
use std::collections::HashSet;
use std::rc::Rc;
fn intern(cache: &mut HashSet<Rc<str>>, data: &str) -> Rc<str> {
match cache.get(data) {
Some(s) => s.clone(),
None => {
let d = Rc::from(data);
cache.insert(d.clone());
d
}
}
}
The current output is:
error[E0282]: type annotations needed
--> src/lib.rs:9:28
|
9 | cache.insert(d.clone());
| ^^^^^ cannot infer type
|
= note: type must be known at this point
error[E0599]: no method named `clone` found for struct `Rc<_>` in the current scope
--> src/lib.rs:9:28
|
9 | cache.insert(d.clone());
| ^^^^^ method not found in `Rc<_>`
|
= note: `d` is a function, perhaps you wish to call it
I'm surprised it can't infer the type of d
correctly, considering there are multiple indications it must be Rc<str>
(and R-A does so). Maybe it's confused by Rc<&str>
being a thing?
The final note is straight up perplexing, however: where on earth is it getting the idea that that's a function from? That should probably be squashed.
Edit: even shorter reproduction (playground):
use std::rc::Rc;
fn intern(data: &str) -> Rc<str> {
Rc::from(data).clone()
}