title: Use an enum with a function which has a slice parameter
body: <p>I use an enum to choose between different function signatures. As long as the function has "normal" (sized) parameters, like <code>u8</code>, this works fine. But as soon as I use a function with a slice as parameter, I get compiler errors. I found a workaround, but I am not sure if this is the only way to solve this. And I don't understand why. Assuming the function is stored as a pointer, how do the parameters change what is possible? </p>
  
  <pre><code>#[derive(Copy, Clone/*, Debug, Eq, PartialEq*/)]
  pub enum Function {
      FunctionVal(fn(u8) -&gt; String),
      //FunctionSlice(fn(&amp;[u8]) -&gt; String), // E0277
      FunctionSlice(&amp;'static fn(&amp;[u8]) -&gt; String), // workaround
  }
  
  #[derive(Copy, Clone/*, Debug, Eq, PartialEq*/)]
  pub struct FunctionStruct {
      pub func: Function,
  }
  
  pub static FUNC1: FunctionStruct = FunctionStruct {
      func: Function::FunctionVal(convert_u8_to_string),
  };
  
  static F2:fn(&amp;[u8]) -&gt; String = convert_u8slice_to_string;
  pub static FUNC2: FunctionStruct = FunctionStruct {
      func: Function::FunctionSlice(&amp;F2), // to avoid E0308
  };
  
  fn convert_u8_to_string(_:u8) -&gt; String { String::from("") }
  fn convert_u8slice_to_string(_:&amp;[u8]) -&gt; String { String::from("") }
  
  fn main() {
      let f = FUNC1;
  
      match f.func {
          Function::FunctionVal(f) =&gt; { f(0); }
          Function::FunctionSlice(f) =&gt; { f(&amp;[0u8]); }
      }
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?gist=0649605edfc82e9f0f1bf0e259bcfebf&amp;version=stable&amp;backtrace=0" rel="nofollow">(Rust Playground)</a></p>
  
  <p>As soon as there is a slice as parameter, <code>#[derive(Debug, Eq, PartialEq)]</code> is not possible any more. But that is not a problem in my case.</p>
  
  <p>I was not able to make <code>FunctionSlice(fn(&amp;[u8]) -&gt; String),</code> work. I had to use a reference type with a static lifetime. Otherwise <code>#[derive(Copy, Clone)]</code> would fail. To initialize the struct, I had to use an additional static.</p>
  
  <p>I am currently using rust 1.10</p>