The goal of the parse_winning_hand
function is to accept a vector of 5 i32
s and then append the corresponding letter to the i'th index of return_val
(from 0 to 5). deal
is a driver function that calls parse_winning_hand
at the end of its run:
fn parse_winning_hand(hand: &Vec<i32>) -> [&str; 5] {
let mut temp_hand = hand.to_vec();
let mut return_val = ["12", "12", "12", "12", "12"];
for i in 0..5 {
let popped = temp_hand.pop().unwrap();
let mut suit = "X";
if popped < 14 {
suit = "C";
} else if popped < 27 {
suit = "D";
} else if popped < 40 {
suit = "H";
} else {
suit = "S";
}
return_val[i] = suit;
}
return return_val;
}
fn deal(arr: &[i32]) -> [&'static str; 5] {
// ...
let decided_winner = decide_winner(hand_one_score, hand_two_score);
if decided_winner == 1 {
let ret = parse_winning_hand(&hand_one);
return ret;
} else {
let ret = parse_winning_hand(&hand_two);
return ret;
}
}
The error that I am getting during compilation is:
error[E0515]: cannot return value referencing local variable `hand_one`
--> Poker.rs:276:10
|
275 | let ret = parse_winning_hand(&hand_one);
| --------- `hand_one` is borrowed here
276 | return ret;
| ^^^ returns a value referencing data owned by the current function
error[E0515]: cannot return value referencing local variable `hand_two`
--> Poker.rs:279:10
|
278 | let ret = parse_winning_hand(&hand_two);
| --------- `hand_two` is borrowed here
279 | return ret;
| ^^^ returns a value referencing data owned by the current function
I have searched for solutions to this problem but either the solution wasn't applicable to my needs or I wasn't able to understand the posted solution due to my lack of knowledge. Why I am getting the error? How do I fix it?