1

My requirement is to return an array of 5 string slices in my calc method. In my main method, I create any arbitrary 10 element array I want for my program.

I pass this array an a parameter to the calc method, it does some calculations, I am just trying to figure out how I can an array of 5 string slices as I am required to.

As I can return either one of two arrays (of type string slices) based on calculations I get, I am trying to return the appropriate array using an if else if ladder.

fn main()
{
  let arr:[u32;10] = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; 
  let output:[&str;5] = calc(arr);
  println!("{:?}", output);

} 


fn calc(arr: [u32;10]) -> [&'static str; 5]
{
    let mut str1:[&str;5] = ["", "", "", "", ""];
    let mut str2:[&str;5] = ["", "", "", "", ""];

    let mut arr1 = [0; 5];
    let mut arr2 = [0; 5];

    let mut sorted_arr1 = arr1;
    let mut sorted_arr2 = arr2;
    sorted_arr1.sort();
    sorted_arr2.sort();

    for i in 0..5
    {
        arr1[i] = arr[i]; 
        arr2[i] = arr[i+5]; 
    }

    //L for less than, E for equal to, G for greater than
    for i in 0..5
    {
      if arr1[i] < 5 {
        str1[i] = &format!("{}{}", arr1[i], "L").to_string();
      }
      else if arr1[i] == 5 { 
        str1[i] = &format!("{}{}", arr1[i], "E").to_string();
      }
      else if arr1[i] > 5  {
        str1[i] = &format!("{}{}", arr1[i], "G").to_string();
      }

      if arr2[i] < 5 {
        str2[i] = &format!("{}{}", arr2[i], "L").to_string();
      }
      else if arr2[i] == 5 {
        str2[i] = &format!("{}{}", arr2[i], "E").to_string();
      }
      else if arr2[i] > 5  {
        str2[i] = &format!("{}{}", arr2[i], "G").to_string();
      }
    }

    let foo1_winner = foo1();   
    let foo2_winner = foo2();   


    if foo1_winner == sorted_arr1 {
        return str1;
    }
    else if foo1_winner == sorted_arr2 {
        return str2;
    }
    else if foo2_winner == sorted_arr1 {
        return str1;
    }
    else if foo2_winner == sorted_arr2 {
        return str2;
    }
    return str1;
}

fn foo1() -> [u32; 5]
{
    [1,2,3,4,5]
}

fn foo2() -> [u32; 5]
{
    [6,7,8,9,10]
}

For all my return statements I get an compilation error like this

/*
error[E0515]: cannot return value referencing temporary value
  --> stack.rs:71:16
   |
43 |         str1[i] = &format!("{}{}", arr1[i], "L").to_string();
   |                    ----------------------------------------- temporary value created here
...
71 |         return str1;
   |                ^^^^ returns a value referencing data owned by the current function
*/

I have searched for solutions to this problem but been able to make sense of anything I have read. How do I fix this issue? Any help is appreciated, thanks.

Robert
  • 43
  • 4
  • Why do you want to return `&'static str` instead of `String`? – loganfsmyth Mar 30 '20 at 06:28
  • 2
    Your strings are newly created in `calc`. Why not just return `[String; 5]`? See [What are the differences between Rust's `String` and `str`?](https://stackoverflow.com/questions/24158114/what-are-the-differences-between-rusts-string-and-str) – SCappella Mar 30 '20 at 06:29
  • My assignment specifies explicitly that I need to return an array of five string slices – Robert Mar 30 '20 at 06:39
  • 1
    Are you sure that you need to return `[ "10G", "9G", … ]` and not just `[ "G", "G", … ]`? The former is impossible, for the latter you'd just use `str1[i] = "G"; …`. – Jmb Mar 30 '20 at 06:48
  • I am sure I am supposed to do the former – Robert Mar 30 '20 at 06:51
  • A `'static` lifetime means that the slice being returned has to live for the entire lifetime of your program, and the only type of slice that meets that requirement is a slice created via a string literal. A String returned from `format()` can never be `'static`. Either you're misunderstanding something, or the assignment is asking for the wrong thing. – loganfsmyth Mar 30 '20 at 06:54
  • I only added the `' static` part because I was getting compilation errors without it, it's not mentioned as a requirement for the assignment. So if it helps my issue by taking it out I don't need it @loganfsmyth – Robert Mar 30 '20 at 07:06
  • @Jmb: It is not impossible if you declare a bunch of static slices: `static G_1: &str = "1G"; static G_2: &str = "2G"; ...` and then return references to these. – rodrigo Mar 30 '20 at 08:18
  • @rodrigo that assumes that all the possible combinations are known beforehand. Strictly speaking, it is also possible by leaking the memory for the slices, but since this is for an assignment, I doubt that the teacher would require this kind of deliberate leaking. – Jmb Mar 30 '20 at 09:51

0 Answers0