I want to identify the common characters in 2 str slices and decided to come up with a function that takes 2 str slices and returns the intersection of the characters of the 2 slices. However I'm fighting with the compiler on this one. Any help? The Iterator return type should not be kept, I just want some type that I can easily manipulate in further functions. (Just started learning Rust :-))
fn common_chars<'a>(s1: &'a str, s2: &'a str) -> impl Iterator<Item = &'a char> {
let a : HashSet<char> = HashSet::from_iter(s1.chars());
let b : HashSet<char> = HashSet::from_iter(s2.chars());
return a.intersection(&b);
}
I'm getting the following error:
mathieu@linda:~/code/aoc/2022/day3 (main)$ cargo build
Compiling day3 v0.1.0 (/home/mathieu/code/aoc/2022/day3)
error[E0515]: cannot return value referencing local variable `b`
--> src/main.rs:19:12
|
19 | return a.intersection(&b);
| ^^^^^^^^^^^^^^^--^
| | |
| | `b` is borrowed here
| returns a value referencing data owned by the current function
|
= help: use `.collect()` to allocate the iterator
error[E0515]: cannot return reference to local variable `a`
--> src/main.rs:19:12
|
19 | return a.intersection(&b);
| ^^^^^^^^^^^^^^^^^^ returns a reference to data owned by the current function