UFC

Nicola Coretti

Dec 15, 2022

Who cares ¯\_(ツ)_/¯

The Language

Safety

“Time turns the improbable into the inevitable” – Unknown

  • out of bound checking
  • no use after free
  • no null pointers
  • no memory leaks
  • under-/overflow checks
  • initalization required!

Const by default

fn main() {
    let var = 1u32;
    var = 10u32;
}
error[E0384]: cannot assign twice to immutable variable `var`
 --> src/main.rs:3:5
  |
2 |     let var = 1u32;
  |         ---
  |         |
  |         first assignment to `var`
  |         help: consider making this binding mutable: `mut var`
3 |     var = 10u32;
  |     ^^^^^^^^^^^ cannot assign twice to immutable variable

For more information about this error, try `rustc --explain E0384`.
error: could not compile `playground` due to previous error; 2 warnings emitted

Destructive Move

fn consume(_data: Vec<u8>) {}

fn main() {
    let data = vec![1u8, 2u8, 3u8];
    consume(data);
    println!("{:?}",data);
}
error[E0382]: borrow of moved value: `data`
 --> src/main.rs:6:21
  |
4 |     let data = vec![1u8, 2u8, 3u8];
  |         ---- move occurs because `data` has type `Vec<u8>`, which does not implement the `Copy` trait
5 |     consume(data);
  |             ---- value moved here
6 |     println!("{:?}",data);
  |                     ^^^^ value borrowed here after move
  |
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` due to previous error

Pattern Matching

match age {
    0..=16 => whatever,
    17..=18 => other_thing,
    _ => another_thing,
}
let kind = match (args.is_present("bin"), args.is_present("lib")) {
    (true, true) => failure::bail!("can't specify both lib and binary outputs"),
    (false, true) => NewProjectKind::Lib,
    (_, false) => NewProjectKind::Bin,
};

Macros

let my_directory = if cfg!(windows) {
    "windows-specific-directory"
} else {
    "unix-directory"
};
fn main() {
    let binary_content = include_bytes!("../Cargo.toml");
    let string_content = include_str!("../Cargo.toml");

    println!("binary content: {:?}", binary_content);
    println!("string content: {:?}", string_content);
}

Attributes

#[cfg(target_family = "windows")]
let my_directory = "windows-specific-directory";

#[cfg(target_family = "unix")]
let my_directory = "unix-directory";
fn main() {

    #[derive(PartialEq, Eq, PartialOrd, Ord)]
    enum E {
        Top,
        Bottom,
    }

    assert!(E::Top < E::Bottom);
}

The stdlib

no_std

no_std

Net

use std::io::prelude::*;
use std::net::TcpStream;

fn main() -> std::io::Result<()> {
    let mut stream = TcpStream::connect("127.0.0.1:34254")?;
    stream.write(b"SPAM!!!")?;
    stream.read(&mut [0; 128])?;
    Ok(())
} // the stream is closed here
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

let addr_v4 = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
let addr_v6 = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);

let addr: SocketAddr = addr_v4.into();

match addr {
    SocketAddr::V4(v) => println!("v4: {:?}", v),
    SocketAddr::V6(v) => println!("v6: {:?}", v),
};

Async

The Ecosystem

Cargo

jack of all trades

Rustup

Now go and don’t look back!

Thanks!