Rust - A thread safe and memory safe language close to metal

This post consists of various links and excerpts that I collected while deciding a language for a project that demanded multi-threading capabilities and low latency. Rust was one of the candidates but I did not go ahead with it. Hence, I have no real programming experience in Rust but the links would prove useful to anyone who wants to know more about Rust, it’s advantages and disadvantages and use cases.

use std::net::{SocketAddrV4, Ipv4Addr, TcpListener};
use std::io::{Read, Error};

fn main() -> Result<(), Error> {
    let loopback = Ipv4Addr::new(127, 0, 0, 1);
    let socket = SocketAddrV4::new(loopback, 0);
    let listener = TcpListener::bind(socket)?;
    let port = listener.local_addr()?;
    println!("Listening on {}, access this port to end the program", port);
    let (mut tcp_stream, addr) = listener.accept()?; //block  until requested
    println!("Connection received! {:?} is sending data.", addr);
    let mut input = String::new();
    let _ = tcp_stream.read_to_string(&mut input)?;
    println!("{:?} says {}", addr, input);
    Ok(())
}

By leveraging ownership and type checking, many concurrency errors are compile-time errors in Rust rather than runtime errors. Therefore, rather than making you spend lots of time trying to reproduce the exact circumstances under which a runtime concurrency bug occurs, incorrect code will refuse to compile and present an error explaining the problem.

Code Samples in Concurrency

  • Multi-threading The move closure is often used alongside thread::spawn because it allows you to use data from one thread in another thread.

Threading Model

Don’t use Rust if you want to get something done quickly. Even with some experience, it’s quite common for me to spend an hour on fixing a single compiler error or data sharing issue that would be a non-issue in something like JavaScript or Python.

In my experience this is half to do with familiarity with Rust, and half to do with correctness.

Things you would likely tweet the day after you start working with Rust

The fight with Borrow Checker has begun

Things you would likely tweet the year after you start working with Rust

Pascal is like wearing a straightjacket, C is like playing with knives, and C++ is juggling flaming chainsaws. In that metaphor, Rust is like doing parkour while suspended on strings & wearing protective gear. Yes, it will sometimes look a little ridiculous, but you’ll be able to do all sorts of cool moves without hurting yourself.