use std::collections::hash_map::{Keys, HashMap};
#[derive(Debug)]
pub struct TopicNames<'a> {
//iter: Keys<'a, String, String>,
iter: Keys<'a, u32, u32>,
}
impl<'a> Iterator for TopicNames<'a> {
//type Item = &'a str;
type Item = &'a u32;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
//self.iter.next().map(AsRef::as_ref)
self.iter.next()
}
}
impl TopicNames<'_>{
pub fn new(&self) ->TopicNames<'_> {
let map = HashMap::from([(1u32, 2u32),]).keys();
TopicNames {iter:map}
}
}
fn main() {
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0515]: cannot return value referencing temporary value
--> src/main.rs:32:8
|
30 | let map = HashMap::from([(1u32, 2u32),]).keys();
| ------------------------------ temporary value created here
31 |
32 | TopicNames {iter:map}
| ^^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
|
= help: use `.collect()` to allocate the iterator
For more information about this error, try `rustc --explain E0515`.
error: could not compile `playground` due to previous error