× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to write a function generic over pointer type?

Chris Morgan
Jul 15, 2015
<p>Both <code>&amp;T</code> and <code>P&lt;T&gt;</code> implement <code>Deref&lt;Target = T&gt;</code>, so you could use that in your bounds:</p> <pre><code>use std::ops::Deref; fn over&lt;X, F, X1, X2&gt;(left: &amp;[X1], right: &amp;[X2], mut eq_fn: F) -&gt; bool where X1: Deref&lt;Target = X&gt;, X2: Deref&lt;Target = X&gt;, F: FnMut(&amp;X, &amp;X) -&gt; bool { left.len() == right.len() &amp;&amp; left.iter().zip(right).all(|(x, y)| eq_fn(x, y)) } </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31430067/How%20to%20write%20a%20function%20generic%20over%20pointer%20type?/31430776">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus