<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="match std::io::stdin().read_u8() {
    Ok(_) =&gt; (),
    Err(e) =&gt; println!(&quot;{}&quot;,e)
}"><pre class="notranslate"><code class="notranslate">match std::io::stdin().read_u8() {
    Ok(_) =&gt; (),
    Err(e) =&gt; println!("{}",e)
}
</code></pre></div>
<p dir="auto">Output: "unknown error (OS Error 8 (FormatMessageW() returned error 15100))"</p>
<p dir="auto">Win 7<br>
rustc 0.10<br>
host: i686-pc-mingw32</p>
<p dir="auto">More investigation from <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="29958060" data-permission-text="Title is private" data-url="https://github.com/rust-lang/rust/issues/13073" data-hovercard-type="issue" data-hovercard-url="/rust-lang/rust/issues/13073/hovercard" href="https://github.com/rust-lang/rust/issues/13073">#13073</a>.</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="extern crate native;
extern crate green;
extern crate rustuv;

use std::libc::{c_void, LPWSTR, LPVOID, DWORD};

extern &quot;system&quot; {
    fn MessageBoxA(hWnd: u32, lpText: *u8, lpCaption: *u8, uType: u32) -&gt; i32;

    fn FormatMessageW(flags: DWORD,
                      lpSrc: LPVOID,
                      msgId: DWORD,
                      langId: DWORD,
                      buf: LPWSTR,
                      nsize: DWORD,
                      args: *c_void)
                      -&gt; DWORD;

    fn GetLastError() -&gt; u32;
}

fn test() {
    let mut buf: [u16, ..50] = [0, ..50];
    let ret = unsafe {
        FormatMessageW(0x1000, 0 as *mut c_void, 1, 0x400,
                       buf.as_mut_ptr(), buf.len() as u32, 0 as *c_void)
    };
    if ret == 0 {
        let err = unsafe { GetLastError() };
        println!(&quot;err: {:?}&quot;, err);
    }
    let s = std::str::from_utf16(buf);
    println!(&quot;{:?}&quot;, s);

    //unsafe {
    //    MessageBoxA(0, &quot;ABC&quot;.as_ptr(), &quot;ABC&quot;.as_ptr(), 0);
    //}
}

fn main() {
    if cfg!(spawn) {
        spawn(proc() {
            test();
        });
    } else {
        test();
    }
}

#[start]
pub fn start(argc: int, argv: **u8) -&gt; int {
    if cfg!(green) {
        green::start(argc, argv, rustuv::event_loop, main)
    } else if cfg!(raw) {
        main();
        0
    } else {
        native::start(argc, argv, main)
    }
}"><pre class="notranslate"><span class="pl-k">extern</span> <span class="pl-k">crate</span> native<span class="pl-kos">;</span>
<span class="pl-k">extern</span> <span class="pl-k">crate</span> green<span class="pl-kos">;</span>
<span class="pl-k">extern</span> <span class="pl-k">crate</span> rustuv<span class="pl-kos">;</span>

<span class="pl-k">use</span> std<span class="pl-kos">::</span>libc<span class="pl-kos">::</span><span class="pl-kos">{</span>c_void<span class="pl-kos">,</span> <span class="pl-v">LPWSTR</span><span class="pl-kos">,</span> <span class="pl-v">LPVOID</span><span class="pl-kos">,</span> <span class="pl-v">DWORD</span><span class="pl-kos">}</span><span class="pl-kos">;</span>

<span class="pl-k">extern</span> <span class="pl-s">"system"</span> <span class="pl-kos">{</span>
    <span class="pl-k">fn</span> <span class="pl-v">MessageBoxA</span><span class="pl-kos">(</span><span class="pl-s1">hWnd</span><span class="pl-kos">:</span> <span class="pl-smi">u32</span><span class="pl-kos">,</span> <span class="pl-s1">lpText</span><span class="pl-kos">:</span> <span class="pl-c1">*</span><span class="pl-smi">u8</span><span class="pl-kos">,</span> <span class="pl-s1">lpCaption</span><span class="pl-kos">:</span> <span class="pl-c1">*</span><span class="pl-smi">u8</span><span class="pl-kos">,</span> <span class="pl-s1">uType</span><span class="pl-kos">:</span> <span class="pl-smi">u32</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">i32</span><span class="pl-kos">;</span>

    <span class="pl-k">fn</span> <span class="pl-v">FormatMessageW</span><span class="pl-kos">(</span><span class="pl-s1">flags</span><span class="pl-kos">:</span> <span class="pl-smi">DWORD</span><span class="pl-kos">,</span>
                      <span class="pl-s1">lpSrc</span><span class="pl-kos">:</span> <span class="pl-smi">LPVOID</span><span class="pl-kos">,</span>
                      <span class="pl-s1">msgId</span><span class="pl-kos">:</span> <span class="pl-smi">DWORD</span><span class="pl-kos">,</span>
                      <span class="pl-s1">langId</span><span class="pl-kos">:</span> <span class="pl-smi">DWORD</span><span class="pl-kos">,</span>
                      <span class="pl-s1">buf</span><span class="pl-kos">:</span> <span class="pl-smi">LPWSTR</span><span class="pl-kos">,</span>
                      <span class="pl-s1">nsize</span><span class="pl-kos">:</span> <span class="pl-smi">DWORD</span><span class="pl-kos">,</span>
                      <span class="pl-s1">args</span><span class="pl-kos">:</span> <span class="pl-c1">*</span><span class="pl-smi">c_void</span><span class="pl-kos">)</span>
                      -&gt; <span class="pl-smi">DWORD</span><span class="pl-kos">;</span>

    <span class="pl-k">fn</span> <span class="pl-v">GetLastError</span><span class="pl-kos">(</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">u32</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">fn</span> <span class="pl-en">test</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> buf<span class="pl-kos">:</span> <span class="pl-kos">[</span><span class="pl-smi">u16</span><span class="pl-kos">,</span> ..<span class="pl-c1">50</span><span class="pl-kos">]</span> = <span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">,</span> ..<span class="pl-c1">50</span><span class="pl-kos">]</span><span class="pl-kos">;</span>
    <span class="pl-k">let</span> ret = <span class="pl-k">unsafe</span> <span class="pl-kos">{</span>
        <span class="pl-v">FormatMessageW</span><span class="pl-kos">(</span><span class="pl-c1">0x1000</span><span class="pl-kos">,</span> <span class="pl-c1">0</span> <span class="pl-k">as</span> <span class="pl-c1">*</span><span class="pl-k">mut</span> <span class="pl-smi">c_void</span><span class="pl-kos">,</span> <span class="pl-c1">1</span><span class="pl-kos">,</span> <span class="pl-c1">0x400</span><span class="pl-kos">,</span>
                       buf<span class="pl-kos">.</span><span class="pl-en">as_mut_ptr</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">,</span> buf<span class="pl-kos">.</span><span class="pl-en">len</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-k">as</span> <span class="pl-smi">u32</span><span class="pl-kos">,</span> <span class="pl-c1">0</span> <span class="pl-k">as</span> <span class="pl-c1">*</span><span class="pl-smi">c_void</span><span class="pl-kos">)</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>
    <span class="pl-k">if</span> ret == <span class="pl-c1">0</span> <span class="pl-kos">{</span>
        <span class="pl-k">let</span> err = <span class="pl-k">unsafe</span> <span class="pl-kos">{</span> <span class="pl-v">GetLastError</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>
        <span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"err: {:?}"</span>, err<span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
    <span class="pl-k">let</span> s = std<span class="pl-kos">::</span>str<span class="pl-kos">::</span><span class="pl-en">from_utf16</span><span class="pl-kos">(</span>buf<span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"{:?}"</span>, s<span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-c">//unsafe {</span>
    <span class="pl-c">//    MessageBoxA(0, "ABC".as_ptr(), "ABC".as_ptr(), 0);</span>
    <span class="pl-c">//}</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">if</span> <span class="pl-en">cfg</span><span class="pl-en">!</span><span class="pl-kos">(</span>spawn<span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-en">spawn</span><span class="pl-kos">(</span><span class="pl-en">proc</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-en">test</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><span class="pl-kos">;</span>
    <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
        <span class="pl-en">test</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>

<span class="pl-c1">#<span class="pl-kos">[</span>start<span class="pl-kos">]</span></span>
<span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">start</span><span class="pl-kos">(</span><span class="pl-s1">argc</span><span class="pl-kos">:</span> <span class="pl-smi">int</span><span class="pl-kos">,</span> <span class="pl-s1">argv</span><span class="pl-kos">:</span> <span class="pl-c1">*</span><span class="pl-c1">*</span><span class="pl-smi">u8</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">int</span> <span class="pl-kos">{</span>
    <span class="pl-k">if</span> <span class="pl-en">cfg</span><span class="pl-en">!</span><span class="pl-kos">(</span>green<span class="pl-kos">)</span> <span class="pl-kos">{</span>
        green<span class="pl-kos">::</span><span class="pl-en">start</span><span class="pl-kos">(</span>argc<span class="pl-kos">,</span> argv<span class="pl-kos">,</span> rustuv<span class="pl-kos">::</span>event_loop<span class="pl-kos">,</span> main<span class="pl-kos">)</span>
    <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-en">cfg</span><span class="pl-en">!</span><span class="pl-kos">(</span>raw<span class="pl-kos">)</span> <span class="pl-kos">{</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-c1">0</span>
    <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
        native<span class="pl-kos">::</span><span class="pl-en">start</span><span class="pl-kos">(</span>argc<span class="pl-kos">,</span> argv<span class="pl-kos">,</span> main<span class="pl-kos">)</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">If the code is bulit with <code class="notranslate">--cfg green</code>, <code class="notranslate">--cfg raw</code> or <code class="notranslate">--cfg spawn</code>, it works as expected. However, if it is built with no cfg, it behaves strangely: <code class="notranslate">FormatMessageW()</code> fails to get system locale mssage, and <code class="notranslate">MessageBoxA()</code> shows gui message box with non-system-themed border.</p>
<p dir="auto">I guess the main task has bad thread-local data due to <code class="notranslate">libnative::start()</code>.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/alexcrichton/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/alexcrichton">@alexcrichton</a></p>