Just An Application

June 10, 2013

Programming With Rust — Part One: So Where Are The Sockets ?

To get some kind of a feel for a new programming language I like to try and use it to write some sort of ‘real-world’-ish program.

After toying with various ideas I decided to try and use Rust to write a very simple HTTP server

Note

All the code that follows was compiled and run using the Mac OS X version of Rust 0.6

How I built Rust 0.6 for Mac OS X is described here.

1.0 So Where Are the Sockets ?

The first thing you need to implement an HTTP server are some sockets, well at least one socket.

Rummaging around in the documentation we find the the following here (re-formatted for clarity)


    fn listen(
           host_ip:         ip::IpAddr, 
           port:            uint, 
           backlog:         uint, 
           iotask:          &IoTask,
           on_establish_cb: ~fn(SharedChan<Option<TcpErrData>>),
           new_connect_cb:  ~fn(TcpNewConnection, SharedChan<Option<TcpErrData>>)) 
		   
       ->  result::Result<(), TcpListenErrData>

So what does all that mean ?

To start with it helps to know how functions are defined in Rust.

2.0 Function Definitions

A function definition in Rust starts with the keyword

    fn

This is followed by the name of the function, then a list of parameters the function takes, then the return type, if any, then the body of the function

Slightly more formally

    functiondef : "fn" ident parameters returntype? body

    parameters  : '(' parameter [',' parameter]* ')' | '()'
    
    parameter   : ident ':' type
    
    returntype  : '->' type

Note

The definition of a parameter is a bit more complicated than that but it will do for now.

3.0 The listen Function Revisited

Given that we now know how functions are defined what we can deduce about the listen function without having
to resort to reading the documentation ?

Not a great deal.

It takes six arguments and returns a value. That is about it.

Everything else is pretty much pure conjecture.

The double colon in the type ip::IpAddr might have something to do with namespaces ?

uints might be unsigned integers ?

The ampersand in &IoTask might have something to do with references, but does Rust even have references ?

Unfortunately compilers are usually not big on conjectures, so there is nothing for it, we are going to have to find stuff out.

4.0 What Do We Need To Know About In Order To Use The listen Function ?

It turns out that to actually use the listen function it is first necessary to know something about all of the
following aspects of Rust

  • some primitive types

  • enum types

  • generic types

  • modules

  • items, expressions, and statements

  • tasks

  • the Rust memory model

  • lambda expressions and closures

For me this is the benefit of trying to do something ‘real-world’ ish when learning a new programming language. Usually you really do have to read the documentation and get to grips with what it says in order to get anything at all to happen !

Update: 27.07.2014

The socket functions as described above no longer exist in Rust.

See here for some discussion of the equivalent functionality in the current version of Rust


Copyright (c) 2013 By Simon Lewis. All Rights Reserved.

Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and owner Simon Lewis is strictly prohibited.

Excerpts and links may be used, provided that full and clear credit is given to Simon Lewis and justanapplication.wordpress.com with appropriate and specific direction to the original content.

Advertisements

1 Comment »


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: