Zero cost abstractions:

I think this is better expressed in text and maybe with a few short examples. Maybe

explain what exactly this means (from example in RFC):

‘

// Closures have stack-allocated environments

// and are statically-dispatched.

’

Not every developer will be coming form a language where understanding the

difference between a stack or heap allocation is critical information. Static

dispatch will be especially confusing to those developers, since the concept

essentially would not exist to someone using Python.

Move semantics:

struct Item { pub inner_value: u32 } fn take_item(item: Item) { println!("Took: {}", item.inner_value); } fn borrow_item(item: &Item) { println!("Borrowed: {}", item.inner_value); } fn main() { let item = Item {inner_value:5}; // 'borrow_item' borrows the item, it takes an immutable reference to it borrow_item(&item); // Prints '5' println!("{}", item.inner_value); // item is moved into function take_item(item); // Compile time error - use of moved value. X was taken by 'take_item'. println!("{}", item.inner_value); }

Guaranteed memory safety:

I think a good example here would have to involve some vulnerable code alongside

some equivalent rust code.

This C code compiles with no warnings on gcc.

#include <stdlib.h> #include <stdio.h> int main(){ int *ptr; // Allocate memory ptr=(int*)malloc(1*sizeof(int)); // Release memory free(ptr); // Use After Free // https://www.owasp.org/index.php/Using_freed_memory printf("Printing a dereferenced pointer after it is free'd %d", ****ptr); // fix pointers, annoying markdown thing // Double-Free // https://www.owasp.org/index.php/Double_Free free(ptr); return 0; }

Rust outright prevents these and much more complex mistakes at compile time

struct Item { inner_value: u32 } fn main() { let item = Item{inner_value: 5}; // Drop is a bit like 'free', when we drop 'item' we are stating it is no longer usable drop(item); // We are prevented, at compile time, from using 'item'. Rust prevents a "use after free" here. println!("{}", item.inner_value); }

Threads without data races:

I’d just show the difference between a single threaded program and a non single

threaded program. It’s barely any extra work to make something parallel in rust.

Trait-based generics:

trait Animal { fn make_noise(&self); } struct Cat; struct Dog; // Implement the trait Animal for Cat impl Animal for Cat { // To satisfy the Animal trait we must implement the make_noise function fn make_noise(&self) { println!("Meow!"); } } // Implement the trait Animal for Cat impl Animal for Dog { fn make_noise(&self) { println!("Woof!"); } } // Takes a reference to anything that implements the Animal trait fn noisy_fn(animal: &Animal) { // Call 'make_noise', which all Animal types must implement animal.make_noise(); } fn main() { // Create our Dog let dog = Dog; // create our cat let cat = Cat; // prints "Woof!" noisy_fn(&dog); // prints "Meow!" noisy_fn(&cat); }

Pattern matching:

I actually think the fizz buzz that uses pattern matching is a really fun example.

Enums(my addiction):

I will also add that after pattern matching would be a good time to show

enums.

Use an example everyone ends up running into,

the ‘Maybe’ or Option type.

// An Optional<T> can either be a Nothing or a 'Something containing a T' pub enum Optional<T> { Nothing, Something(T), } fn main() { let nothing : Optional<&str> = Optional::Nothing; let something = Optional::Something("hello"); let vec = vec![nothing, something]; for thing in vec { // You can pattern match against enums. match thing { Optional::Nothing => println!("Nothing"), Optional::Something(value) => println!("Something: {}", value) } } }

The last 3 are ok but idk of good ways to show them.

People who are going to be looking at these will be new people to the language. So the examples should be as small as possible, and only introduce a single concept at a time if possible.

I think @mrmonday is correct - showing off the environment is important. Cargo is such a huge tool if you’re coming from C++, or even Python/ languages with not-amazing package management. There are thousands of crates, maybe taking a few of the ‘crate of the weeks’ and displaying them would be a good idea, or somehow expressing that rust is usable for a lot of projects already.

Linking to rustbyexample.com would also be a good idea. Since it’s basically just page after page of examples.