0

The goal of the parse_winning_hand function is to accept a vector of 5 i32s 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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
AFC
  • 181
  • 1
  • 11
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what things are present in the code, like `decide_winner` or `hand_one_score`. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Nov 20 '19 at 03:38
  • There's a number of non-idiomatic aspects to this code. Once you have a working solution, I encourage you to seek out holistic review of your code on [Code Review](https://codereview.meta.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users). – Shepmaster Nov 20 '19 at 03:48

1 Answers1

0

Your example can be reduced to this:

fn parse_winning_hand(_: &[i32]) -> [&str; 1] {
    ["0"]
}

fn deal() -> [&'static str; 1] {
    parse_winning_hand(&vec![])
}
error[E0515]: cannot return value referencing temporary value
 --> src/lib.rs:6:5
  |
6 |     parse_winning_hand(&vec![])
  |     ^^^^^^^^^^^^^^^^^^^^------^
  |     |                   |
  |     |                   temporary value created here
  |     returns a value referencing data owned by the current function
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

This is because you have forgotten to specify the 'static lifetime on the return type of parse_winning_hand:

fn parse_winning_hand(_: &[i32]) -> [&'static str; 1]
//                                    ^~~~~~~

Without the 'static, lifetime elision causes the function signature to tie the lifetime of the returned value to the lifetime of the input value:

fn parse_winning_hand<'a>(_: &'a [i32]) -> [&'a str; 1]

This means that it's impossible for the returned value to outlive the Vec that is passed in.

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366