Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Rust and the match statement how can I assign a TcpListener?
    primarykey
    data
    text
    <p>I'm learning Rust in my spare time, and I'm having trouble finding the answer to this problem. The code below which has been slightly modified appeared on Stack-O in answer to another question.I have split lines 9 and 14 for my testing purposes, but that is not essential.</p> <p>What I would like to do is to use the match statement to assign the variable from line 8/9, or from lines 8/9 and 14 combined would also be suitable.</p> <p>Could someone please show me how to this using the match statement?</p> <pre><code>001 use std::cell::Cell; 002 use std::rt::io::{Writer, Listener, Acceptor}; 003 use std::rt::io::net::tcp::TcpListener; 004 use std::rt::io::net::ip::{SocketAddr, Ipv4Addr}; 005 006 fn main() { 007 008 let o_listener = TcpListener::bind( 009 SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 9123}).listen(); 010 011 print ("Listener opened : "); 012 std::io::stdin().read_line(); 013 014 let mut o_acceptor = o_listener.unwrap(); 015 016 println("listener is ready"); 017 loop { 018 let stream = Cell::new(o_acceptor.accept().unwrap()); 019 do spawn { 020 let mut stream = stream.take(); 021 stream.write(bytes!("Hello World\r\n")); 022 } 023 } 024 } </code></pre> <p>Example Only: For example using something like the following, but obviously for the above problem:</p> <pre><code>extern mod sqlite; fn db() { let database = match sqlite::open("test.db") { Ok(result) =&gt; result, Err(error) =&gt; { println(fmt!("Error opening test.db: %?", error)); return; } }; </code></pre> <p>Edited Update : 12 hours after posting question <strong><em>*</em></strong></p> <p>I'm using 0.8 on Win8</p> <p>The following is an example of the problem that I would very-much like a solution to :</p> <pre><code>001 use std::cell::Cell; 002 use std::rt::io::{Writer, Listener, Acceptor}; 003 use std::rt::io::net::tcp::TcpListener; 004 use std::rt::io::net::ip::{SocketAddr, Ipv4Addr}; 005 006 fn main() { 007 008 // This works : 009 // let o_listener = TcpListener::bind( 010 // SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 9123}).listen(); 011 012 // This doesn't work, and results in compile errors below: 013 let o_listener = match TcpListener::bind( 014 SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 9123}).listen() { 015 Some(result) =&gt; result, 016 None =&gt; fail!("Failed to open listener") 017 }; 018 019 // this works in combination with lines 9/10 020 let mut o_acceptor = match o_listener { 021 Some(result) =&gt; result, 022 None =&gt; fail!("Failed to open acceptor.") 023 }; </code></pre> <p>The attempt to compile the above results in (part only) :</p> <pre><code>test_tcp_008.rs:20:8: 20:20 error: mismatched types: expected `std::rt::io::net: :tcp::TcpAcceptor` but found `std::option::Option&lt;&lt;V21&gt;&gt;` (expected struct std:: rt::io::net::tcp::TcpAcceptor but found enum std::option::Option) test_tcp_008.rs:20 Some(result) =&gt; result, </code></pre> <p>This also does not work (compile error) :</p> <pre><code>fn main() { let mut o_acceptor = match TcpListener::bind( SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 9123}) .listen().unwrap() { Some(result) =&gt; result, None =&gt; fail!("Failed to open Listener/Acceptor.") }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload