<p dir="auto">Here is a very simple program which employs trait objects:</p>
<p dir="auto"><code class="notranslate">c.rc</code>:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#[crate_id = &quot;c#0.1&quot;];
#[crate_type = &quot;bin&quot;];

mod m1;
mod m2;"><pre class="notranslate"><span class="pl-c1">#<span class="pl-kos">[</span>crate_id = <span class="pl-s">"c#0.1"</span><span class="pl-kos">]</span></span><span class="pl-kos">;</span>
<span class="pl-c1">#<span class="pl-kos">[</span>crate_type = <span class="pl-s">"bin"</span><span class="pl-kos">]</span></span><span class="pl-kos">;</span>

<span class="pl-k">mod</span> m1<span class="pl-kos">;</span>
<span class="pl-k">mod</span> m2<span class="pl-kos">;</span></pre></div>
<p dir="auto"><code class="notranslate">m1.rs</code>:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="use std::io;

pub struct Container&lt;'a&gt; {
    priv reader: &amp;'a mut Reader
}

impl&lt;'a&gt; Container&lt;'a&gt; {
    pub fn wrap&lt;'s&gt;(reader: &amp;'s mut Reader) -&gt; Container&lt;'s&gt; {
        Container { reader: reader }
    }

    pub fn read_to(&amp;self, vec: &amp;mut [u8]) {
        self.reader.read(vec);
    }
}

pub fn for_stdin&lt;'a&gt;() -&gt; Container&lt;'a&gt; {
    let mut r = io::stdin();
    Container::wrap(&amp;mut r as &amp;mut Reader)
}"><pre class="notranslate"><span class="pl-k">use</span> std<span class="pl-kos">::</span>io<span class="pl-kos">;</span>

<span class="pl-k">pub</span> <span class="pl-k">struct</span> <span class="pl-smi">Container</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    priv <span class="pl-c1">reader</span><span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-c1">'</span><span class="pl-ent">a</span> <span class="pl-k">mut</span> <span class="pl-smi">Reader</span>
<span class="pl-kos">}</span>

<span class="pl-k">impl</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">&gt;</span> <span class="pl-smi">Container</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">wrap</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">s</span><span class="pl-kos">&gt;</span><span class="pl-kos">(</span><span class="pl-s1">reader</span><span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-c1">'</span><span class="pl-ent">s</span> <span class="pl-k">mut</span> <span class="pl-smi">Reader</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">Container</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">s</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
        <span class="pl-smi">Container</span> <span class="pl-kos">{</span> <span class="pl-c1">reader</span><span class="pl-kos">:</span> reader <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>

    <span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">read_to</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-smi">self</span><span class="pl-kos">,</span> <span class="pl-s1">vec</span><span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-kos">[</span><span class="pl-smi">u8</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-smi">self</span><span class="pl-kos">.</span><span class="pl-c1">reader</span><span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span>vec<span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">for_stdin</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">&gt;</span><span class="pl-kos">(</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">Container</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> <span class="pl-k">mut</span> r = io<span class="pl-kos">::</span><span class="pl-en">stdin</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-smi">Container</span><span class="pl-kos">::</span><span class="pl-en">wrap</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-k">mut</span> r <span class="pl-k">as</span> <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">Reader</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><code class="notranslate">m2.rs</code>:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="use std::vec;

use m1;

#[main]
fn main() {
    let c = m1::for_stdin();
    let mut v = vec::from_elem(10, 0u8);
    c.read_to(v.as_mut_slice());
}"><pre class="notranslate"><span class="pl-k">use</span> std<span class="pl-kos">::</span>vec<span class="pl-kos">;</span>

<span class="pl-k">use</span> m1<span class="pl-kos">;</span>

<span class="pl-c1">#<span class="pl-kos">[</span>main<span class="pl-kos">]</span></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> c = m1<span class="pl-kos">::</span><span class="pl-en">for_stdin</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">let</span> <span class="pl-k">mut</span> v = vec<span class="pl-kos">::</span><span class="pl-en">from_elem</span><span class="pl-kos">(</span><span class="pl-c1">10</span><span class="pl-kos">,</span> <span class="pl-c1">0u8</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    c<span class="pl-kos">.</span><span class="pl-en">read_to</span><span class="pl-kos">(</span>v<span class="pl-kos">.</span><span class="pl-en">as_mut_slice</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">When this program is compiled and run, it fails with segmentation fault:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="% rustc c.rc
% ./c
zsh: segmentation fault (core dumped)  ./c"><pre class="notranslate"><code class="notranslate">% rustc c.rc
% ./c
zsh: segmentation fault (core dumped)  ./c
</code></pre></div>
<p dir="auto">This program is not correct. You can see that <code class="notranslate">m1::for_stdin()</code> function produces a <code class="notranslate">Container&lt;'a, ...&gt;</code> for arbitrary lifetime <code class="notranslate">'a</code>, but the local variable <code class="notranslate">r</code> is valid only for <code class="notranslate">for_stdin()</code> body. A pointer to <code class="notranslate">r</code>, however, is saved to the <code class="notranslate">Container</code>, and when it is accessed inside <code class="notranslate">m2::main()</code>, a segmentation fault occurs. However, Rust still compiles it. I thought that borrowing checker should prevent this kind of errors.</p>
<p dir="auto">BTW, I can write <code class="notranslate">&amp;mut r</code> or <code class="notranslate">&amp;mut r as &amp;mut Reader</code> and it will compile either way, but I thought that when going from plain pointers to trait objects I should always specify exact kind of trait object. Not sure that I'm right here thought.</p>
<p dir="auto">Currently obj constructors capture type parameters, but binds don't. Fix this.</p>
<p dir="auto">(Possibly share more code with obj constructors? They're doing quite a bit of the same work, seems a shame to have so much duplication)</p>