1 // Issue 52126: With respect to variance, the assign-op's like += were
2 // accidentally lumped together with other binary op's. In both cases
3 // we were coercing the LHS of the op to the expected supertype.
5 // The problem is that since the LHS of += is modified, we need the
6 // parameter to be invariant with respect to the overall type, not
9 use std
::collections
::HashMap
;
10 use std
::ops
::AddAssign
;
16 pub struct Counter
<'l
> {
17 map
: HashMap
<&'l
str, usize>,
20 impl<'l
> AddAssign
for Counter
<'l
>
22 fn add_assign(&mut self, rhs
: Counter
<'l
>) {
23 rhs
.map
.into_iter().for_each(|(key
, val
)| {
24 let count
= self.map
.entry(key
).or_insert(0);
30 /// Often crashes, if not prints invalid strings.
32 let mut acc
= Counter{map: HashMap::new()}
;
33 for line
in vec
!["123456789".to_string(), "12345678".to_string()] {
34 let v
: Vec
<&str> = line
.split_whitespace().collect();
35 //~^ ERROR `line` does not live long enough
36 // println!("accumulator before add_assign {:?}", acc.map);
37 let mut map
= HashMap
::new();
39 let e
= map
.entry(str_ref
);
40 println
!("entry: {:?}", e
);
41 let count
= e
.or_insert(0);
44 let cnt2
= Counter{map}
;
46 // println!("accumulator after add_assign {:?}", acc.map);
47 // line gets dropped here but references are kept in acc.map