One side of a conversation about Rust-lang.

Tags: software-dev · Rust

Rust-lang

“Huh? Rust is a statically and strongly typed systems programming language? So what? I have c and c++ for that kind of thing. They offer extreme portability (c especially). Why would I want anything else?”

“Oh right, memory safety. I see. That’s a compelling argument. Or, it would be… Its 2022! We have great features built into our wonderful C++ these days. Smart pointers and references are all I need.”

“Oh, lifetimes? That… seems pretty cool actually. Having some kind of promise that a reference will live as long as I’m using it is quite nice actually. But.. smart pointers help with that!”

“Wait, by default everything gets moved? What madness is that? Oh efficiency and safety? Oh that’s nice.””

“Optional results? We have that in C++. That’s not what you mean? Well what do you mean?”

fn some_function_call() -> Result<i32, SomeError> {
	some_other_function_call("some parameter value")?
}

“Oh Optional and Result are error-encapsulating types. I see. That question mark syntax does really look handy. It’ll remove a lot of try ... catch verbosity.”

if let Ok(result) = some_function_call() {
	do_something_with(result);
}

“What madness is that? You can’t do that! Where’s the parentheses around the condition? This is some blasphemous stuff. I’ll tell Knuth on you! - Hold on, did you just de-structure that Result into its value? Oh that is really nice.”

enum Results {
	Completed,
	SortOfCompleted(i32, String),
	Failed(String)
}

let progress = match do_something_get_result() -> i32{
	Results::Completed => 100,
	Results::SortOfCompleted(percentage, reason) => {
		println!("Only got {}% of the way there, because {}.", percentage, reason);
		percentage
	},
	Results::Failed(reason) => {
		println!("Couldn't get anywhere because {}", reason);
		0
	}
}

enum values are structs I see, and that match stuff is quite compelling too. What about templating? C++ has a whole bunch of excellent meta-programming available?”

fn incr<T>(x: T) -> T {
	x + 1
}

struct TwoTuple<T, U> (T, U)

“Cool!”

#[derive(Copy)]
struct Point {
	x: i32,
	y: i32
}

struct NonCopyingPoint {
	x: i32,
	y: i32
}

fn main() {
	let p1 = Point{x:0, y:0};
	let p1_c = p1; // allowed

	let p2 = NonCopyingPoint{x:0, y:0};
	let p2_c = p2; // error - NonCopyingPoint can't be copied!
}

“Wait what’s that now? Proper code generation support? No more hacking together macros to write out boilerplate code - I can just derive the trait for each struct? Excellent! There’s more? Stop! I’m already convinced!

And so began my journey into rust.


Questions? Comments? Get in touch on Twitter!