title: Can a trait guarantee certain type properties such as a vector is non-empty?
body: <p>Imagine I have functions like this:</p>
  
  <pre><code>fn min_max_difference(row: &amp;Vec&lt;u32&gt;) -&gt; u32 {
      let mut min_elem: u32 = row[0];
      let mut max_elem: u32 = min_elem;
  
      for &amp;element in row.iter().skip(1) {
          if element &lt; min_elem {
              min_elem = element;
          } else if element &gt; max_elem {
              max_elem = element;
          }
      }
  
      result = max_elem - min_elem;
  }
  
  fn execute_row_operation(row: &amp;Vec&lt;u32&gt;, operation: Fn(&amp;Vec&lt;u32&gt;) -&gt; u32) -&gt; Option&lt;(u32, u32)&gt; {
      let mut result = None;
  
      if row.len() &gt; 0 {
          result = operation(row);
      }
  
      result
  }
  </code></pre>
  
  <p>Note that the <code>if</code> block in <code>execute_row_operation</code> guarantees that the <code>Vec&lt;u32&gt;</code> I am passing to the <code>operation</code> function is non-empty. In general, I want "operations" to be functions which <em>only</em> accept non-empty rows. I would like it if I could do something like this:</p>
  
  <pre><code>fn min_max_difference&lt;T: &amp;Vec&lt;u32&gt; + NonEmpty&gt;(row: T) -&gt; u32 {
      //snip
  }
  </code></pre>
  
  <p>This would allow the compiler to disallow passing references to empty vectors to a function like <code>min_max_difference</code> which expects this. </p>
  
  <p>But <a href="https://rustbyexample.com/trait.html" rel="noreferrer">traits as I understand them</a> specify what methods a type has, rather than what properties a type has. In my head, I am imagining a trait for a type <code>T</code> that is composed of boolean predicates with type: <code>Fn&lt;T&gt; -&gt; bool</code>, and such a trait is "implemented" for a type if it all those predicates evaluate to true. </p>
  
  <p>Can something like this be achieved?</p>