Can't push to a Vector

โš“ Rust    ๐Ÿ“… 2025-12-23    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 4      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Can't push to a Vector

Hello,
I'm learning rust, in the end of chapter 8.3, there's some suggested projects to build. I'm doing the last one which is:

Using a hash map and vectors, create a text interface to allow a user to add employee names to a department in a company; for example, โ€œAdd Sally to Engineeringโ€ or โ€œAdd Amir to Sales.โ€ Then let the user retrieve a list of all people in a department or all people in the company by department, sorted alphabetically.

but when I'm trying to push a name to the vector in the hashmap, I get the following error:

error[E0614]: type `()` cannot be dereferenced
  --> src/main.rs:12:25
   |
12 |         .and_modify(|e| *e.push(name))
   |                         ^^^^^^^^^^^^^ can't be dereferenced

For more information about this error, try `rustc --explain E0614`.

and If I remove the * the error gets weirder:

error: lifetime may not live long enough                                                                                                                                                    
  --> src/main.rs:10:5                                                                                                                                                                      
   |                                                                                                                                                                                        
 9 |   fn add(departments: &mut HashMap<&str, Vec<&str>>, department_name: &str, name: &str) -> () {                                                                                        
   |                                    -                                  - let's call the lifetime of this reference `'1`                                                                 
   |                                    |                                                                                                                                                   
   |                                    let's call the lifetime of this reference `'2`                                                                                                      
10 | /     departments                                                                                                                                                                      
11 | |         .entry(department_name)                                                                                                                                                      
   | |_______________________________^ argument requires that `'1` must outlive `'2`                                                                                                        
   |                                                                                                                                                                                        
   = note: requirement occurs because of a mutable reference to `HashMap<&str, Vec<&str>>`                                                                                                  
   = note: mutable references are invariant over their type parameter                                                                                                                       
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance                                                                                       
help: consider introducing a named lifetime parameter                                                                                                                                       
   |                                                                                                                                                                                        
 9 | fn add<'a>(departments: &mut HashMap<&'a str, Vec<&str>>, department_name: &'a str, name: &str) -> () {                                                                                
   |       ++++                            ++                                    ++                                                                                                         
                                                                                                                                                                                            
error: lifetime may not live long enough                                                                                                                                                    
  --> src/main.rs:10:5                                                                                                                                                                      
   |                                                                                                                                                                                        
 9 |   fn add(departments: &mut HashMap<&str, Vec<&str>>, department_name: &str, name: &str) -> () {                                                                                        
   |                                              -                                    - let's call the lifetime of this reference `'3`                                                     
   |                                              |                                                                                                                                         
   |                                              let's call the lifetime of this reference `'4`                                                                                            
10 | /     departments                                                                                                                                                                      
11 | |         .entry(department_name)                                                                                                                                                      
   | |_______________________________^ argument requires that `'3` must outlive `'4`                                                                                                        
   |                                                                                                                                                                                        
   = note: requirement occurs because of a mutable reference to `HashMap<&str, Vec<&str>>`                                                                                                  
   = note: mutable references are invariant over their type parameter                                                                                                                       
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance                                                                                       
help: consider introducing a named lifetime parameter                                                                                                                                       
   |                                                                                                                                                                                        
 9 | fn add<'a>(departments: &mut HashMap<&str, Vec<&'a str>>, department_name: &str, name: &'a str) -> () {                                                                                
   |       ++++                                      ++                                      ++                                                                                             
                                                                                                                                                                                            
error: could not compile `company_map` (bin "company_map") due to 2 previous errors

Here's my code:

use std::collections::HashMap;
fn main() {
    let mut departments: HashMap<&str, Vec<&str>> = HashMap::new();
    add(&mut departments, "Engineering", "Amir");
    println!("{departments:?}")
}

fn add(departments: &mut HashMap<&str, Vec<&str>>, department_name: &str, name: &str) -> () {
    departments
        .entry(department_name)
        .and_modify(|e| *e.push(name))
        .or_insert(vec![name]);
}

Thanks!

5 posts - 4 participants

Read full topic

๐Ÿท๏ธ Rust_feed