u/IllustriousTime6197

▲ 42 r/Xreal+1 crossposts

better practice &str or String as function arg

Hello,

what is a better practice? and why? Assume the key I want to insert is available in String type initially.

use std::collections::HashMap;

    let mut map = HashMap::<String, u32>::new();

    // a fct to add a key,value to my hashmap (I know it doesn't need to be      implemented in this example but in my use case it does).

    fn add_pair_v1(map: &mut HashMap::<String, u32>, s:String, u:u32){
      map.insert(s,u);
    }
    
    //or
    
    fn add_pair_v2(map:&mut HashMap::<String, u32>, s:&str, u:u32){
      map.insert(s.to_string(),u);
    }

Thank you !

reddit.com
u/IllustriousTime6197 — 2 days ago