<p dir="auto">Multiply only works when called directly or when inverted (<code class="notranslate">len * 3.0</code>).</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#[deriving(Copy, Show)]
pub struct Len(f64);

impl Mul&lt;Len, Len&gt; for f64 {
    fn mul(self, r: Len) -&gt; Len {
        let Len(right) = r;

        Len(self * right)
    }
}

fn main() {
    let len = Len(12.0);

    // works
    println!(&quot;{}&quot;, 3.0.mul(len));
    // doesn't
    println!(&quot;{}&quot;, 3.0 * len);
}"><pre class="notranslate"><span class="pl-c1">#<span class="pl-kos">[</span>deriving<span class="pl-kos">(</span><span class="pl-v">Copy</span>, <span class="pl-v">Show</span><span class="pl-kos">)</span><span class="pl-kos">]</span></span>
<span class="pl-k">pub</span> <span class="pl-k">struct</span> <span class="pl-smi">Len</span><span class="pl-kos">(</span><span class="pl-smi">f64</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-k">impl</span> <span class="pl-smi">Mul</span><span class="pl-kos">&lt;</span><span class="pl-smi">Len</span><span class="pl-kos">,</span> <span class="pl-smi">Len</span><span class="pl-kos">&gt;</span> <span class="pl-k">for</span> <span class="pl-smi">f64</span> <span class="pl-kos">{</span>
    <span class="pl-k">fn</span> <span class="pl-en">mul</span><span class="pl-kos">(</span><span class="pl-smi">self</span><span class="pl-kos">,</span> <span class="pl-s1">r</span><span class="pl-kos">:</span> <span class="pl-smi">Len</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">Len</span> <span class="pl-kos">{</span>
        <span class="pl-k">let</span> <span class="pl-v">Len</span><span class="pl-kos">(</span>right<span class="pl-kos">)</span> = r<span class="pl-kos">;</span>

        <span class="pl-v">Len</span><span class="pl-kos">(</span><span class="pl-smi">self</span> <span class="pl-c1">*</span> right<span class="pl-kos">)</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> len = <span class="pl-v">Len</span><span class="pl-kos">(</span><span class="pl-c1">12.0</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-c">// works</span>
    <span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"{}"</span>, <span class="pl-c1">3.0</span>.mul<span class="pl-kos">(</span>len<span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-c">// doesn't</span>
    <span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"{}"</span>, <span class="pl-c1">3.0</span> * len<span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<h3 dir="auto">STR</h3>
<p dir="auto">Given the proper (commutative) <code class="notranslate">Add</code>/<code class="notranslate">Mul</code> implementations. (See this <a href="http://is.gd/HsB5yl" rel="nofollow">playpen link</a>)</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const I: Complex&lt;f32&gt; = Complex { re: 0., im: 1. };

let z = I * 3.;  // OK
let z = I + 3.;  // OK
let z = 3. * I;  // Err
let z = 3. + I;  // Err"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-v">I</span><span class="pl-kos">:</span> <span class="pl-smi">Complex</span><span class="pl-kos">&lt;</span><span class="pl-smi">f32</span><span class="pl-kos">&gt;</span> = <span class="pl-smi">Complex</span> <span class="pl-kos">{</span> <span class="pl-c1">re</span><span class="pl-kos">:</span> <span class="pl-c1">0.</span><span class="pl-kos">,</span> <span class="pl-c1">im</span><span class="pl-kos">:</span> <span class="pl-c1">1.</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>

<span class="pl-k">let</span> z = <span class="pl-v">I</span> <span class="pl-c1">*</span> <span class="pl-c1">3.</span><span class="pl-kos">;</span>  <span class="pl-c">// OK</span>
<span class="pl-k">let</span> z = <span class="pl-v">I</span> + <span class="pl-c1">3.</span><span class="pl-kos">;</span>  <span class="pl-c">// OK</span>
<span class="pl-k">let</span> z = <span class="pl-c1">3.</span> <span class="pl-c1">*</span> <span class="pl-v">I</span><span class="pl-kos">;</span>  <span class="pl-c">// Err</span>
<span class="pl-k">let</span> z = <span class="pl-c1">3.</span> + <span class="pl-v">I</span><span class="pl-kos">;</span>  <span class="pl-c">// Err</span></pre></div>
<h3 dir="auto">Output</h3>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="error: mismatched types: expected `_`, found `Complex&lt;f32&gt;` (expected floating-point variable, found struct Complex)"><pre class="notranslate">error<span class="pl-kos">:</span> mismatched types<span class="pl-kos">:</span> expected `_`<span class="pl-kos">,</span> found `<span class="pl-v">Complex</span>&lt;f32&gt;` <span class="pl-kos">(</span>expected floating-point variable<span class="pl-kos">,</span> found struct <span class="pl-v">Complex</span><span class="pl-kos">)</span></pre></div>
<p dir="auto"><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/nikomatsakis/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/nikomatsakis">@nikomatsakis</a> Could this work in the future? It would be great if could type <code class="notranslate">3. + 4. * I</code> instead of <code class="notranslate">I * 4. + 3.</code>, the former feels more natural.</p>