title: How to return multiple types from a function?
body: <p>I'm using <a href="https://docs.rs/actix-web/3.2.0/actix_web/" rel="nofollow noreferrer">actix_web</a> for my handlers and I've stumbled across a problem.</p>
  <p>I'd like to return a <code>web::Json</code> with 1 out of 2 types depending on the parameters.</p>
  <p>I have something like this which doesn't compile because <code>impl</code> can only be one type</p>
  <pre class="lang-rust prettyprint-override"><code>#[derive(Deserialize)]
  pub struct TerraformFieldParams {
      field: String,
  }
  
  #[derive(Serialize)]
  struct TerraformFieldResponse {
      fieldName: String,
      fieldId: String,
      dataType: String,
      state: String,
  }
  
  #[derive(Serialize)]
  struct TerraformFieldErrorResponse {
      error: String,
  }
  
  pub async fn handler(params: web::Path&lt;TerraformFieldParams&gt;) -&gt; impl actix_web::Responder {
      match &amp;params.field[..] {
          &quot;0000000006549B31&quot; =&gt; web::Json(TerraformFieldResponse {
              fieldName: String::from(&quot;host&quot;),
              fieldId: String::from(&quot;000000123&quot;),
              dataType: String::from(&quot;String&quot;),
              state: String::from(&quot;Enabled&quot;),
          }),
  
          _ =&gt; web::Json(TerraformFieldErrorResponse {
              err: String::from(&quot;error 123&quot;),
          }),
      }
  }
  </code></pre>
  <p>How can I do this?</p>