You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How this presentation works. Interactiveness explained. Large credit to RustByExample.com
Link to presentation.
Goals of presentation.
Things that are covered.
1. Explain what Rust is and where it comes from.
1. Offer basic examples of all core aspects of language.
Things that aren't covered.
1. Cargo.
1. How to organize projects.
What is Rust.
Photo from rustcamp (Rust is X without Y).
Learning basics.
Hello world.
fnmain(){println!("Hello World!");}
Printing options.
fnmain(){println!("{} days", 31);println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");println!("{subject} {verb} {predicate}",
predicate="over the lazy dog",
subject="the quick brown fox",
verb="jumps");println!("{} of {:b} people know binary, the other half don't", 1, 2);println!("My name is {0}, {1} {0}", "Bond", "James");}
Printing complex things.
structStructure(i32);fnmain(){// Custom types such as this structure require more complicated// handling. This will not work.println!("This struct `{}` won't print...", Structure(3));}
Printing complex things.
#[derive(Debug)]structStructure(i32);fnmain(){// Printing with `{:?}` is similar to with `{}`.println!("{:?} months in a year.", 12);println!("{1:?} {0:?} is the {actor:?} name.",
"Slater",
"Christian",
actor="actor's");// `Structure` is printable!println!("Now {:?} will print!", Structure(3));}
Custom printing, imports, traits.
// Import (via `use`) the `fmt` module to make it available.use std::fmt;// Define a structure which `fmt::Display` will be implemented for. This is simply// a tuple struct containing an `i32` bound to the name `Structure`.structStructure(i32);// In order to use the `{}` marker, the trait `fmt::Display` must be implemented// manually for the type.impl fmt::DisplayforStructure{// This trait requires `fmt` with this exact signature.fnfmt(&self,f:&mut fmt::Formatter) -> fmt::Result{// Write strictly the first element into the supplied output// stream: `f`. Returns `fmt::Result` which indicates whether the// operation succeeded or failed. Note that `write!` uses syntax which// is very similar to `println!`.write!(f, "{}", self.0)}}