there is a python function I want to translate it to rust:
def artist_albums(self, artist_id, album_type=None, country=None, limit=20,
offset=0):
params= dict()
if album_type:
params['album_type']=album_type
if country:
params['country']=country
params['limit']=limit
params['offset']=offset
trid = self._get_id('artist', artist_id)
return self._get('artists/' + trid + '/albums', params)
this is my rust version
pub fn artist_albums(&self,
artist_id: &mut str,
album_type: Option<ALBUM_TYPE>,
country: Option<&str>,
limit: Option<u32>,
offset: Option<u32>)
-> Option<Albums> {
let mut params: HashMap<&str, String> = HashMap::new();
if let Some(_limit) = limit {
params.insert("limit", _limit.to_string());
}
if let Some(_album_type) = album_type {
params.insert("album_type", _album_type.as_str().to_owned());
}
if let Some(_offset) = offset {
params.insert("offset", _offset.to_string());
}
if let Some(_country) = country {
params.insert("country", _country.to_string());
}
let trid = self.get_id(TYPE::ARTIST, artist_id);
let mut url = String::from("artists/");
url.push_str(&trid);
url.push_str("/albums");
match self.get(&mut url, None, &mut params) {
Some(result) => {
// let mut albums: Albums = ;
match serde_json::from_str::<Albums>(&result) {
Ok(_albums) => Some(_albums),
Err(why) => {
eprintln!("convert albums from String to Albums failed {:?}", why);
None
}
}
}
None => None,
}
}
But
if let Some(_country) = offset {
params.insert("country", _country.to_string());
}
are not equal to
if country:
params['country']=country
it seems
if let Some(_country) = offset {
params.insert("country", _country.to_string());
}//=> lifetime ends here, so params will act like it hasn't insert anything ?
This is the complete version, I am going to build the Spotify api wrapper in Rust, translated from Python version