I'm new in Rust, I'm doing my first steps by resolving exercises on https://exercism.org/.
I'm creating a method that finds a MatchDetails
in a vector, if it exists then returns the reference of this instance otherwise returns the reference of a new instance of MatchDetails
.
It happens that MatchDetails::with_team_name(team_name)
returns a new instance of MatchDetails
based on a team_name
and I want to use the reference returns from this MatchDetails::with_team_name(team_name)
.
pub fn get_team(match_details: &Vec<MatchDetails>, team_name: String) -> &MatchDetails {
let team = match_details.iter().find(|md| md.team_name == team_name);
match team {
Some(match_detail) => match_detail,
None => MatchDetails::with_team_name(team_name),
}
}
In the None
match I would like to return something like &MatchDetails::with_team_name(team_name)
but I get the following error:
cannot return value referencing temporary value returns a value referencing data owned by the current function