Warning: Language nerd presenting
Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.
What am I selling?
fn main() {
println!("Hello World!");
}struct Person {
name: String
}
impl Person {
fn greet(&self) {
println!("Hello {}", self.name)
}
}
fn main() {
let dylan = Person { name: String::from("Dylan") };
dylan.greet();
}
fn main() {
let mut number = 0;
number += 1;
}Disclaimer: This is according to me
fn string_length(input: &String) -> usize {
input.len()
}References
fn shout(input: &mut String) {
input.push_str("!");
}fn string_length(input: &String) -> usize {
input.len()
}
fn shout(input: &mut String) {
input.push_str("!");
}
fn main() {
let name = String::from("Dylan");
let length = string_length(&name);
println!("{}", length);
shout(&mut name); // Compilation error
println!("{}", name);
}References
error[E0596]: cannot borrow immutable local variable `name` as mutable
--> src/main.rs:84:16
|
80 | let name = String::from("Dylan");
| ---- consider changing this to `mut name`
...
84 | shout(&mut name);
| ^^^^ cannot borrow mutablyfn string_length(input: &String) -> usize {
input.len()
}
fn shout(input: &mut String) {
input.push_str("!");
}
fn main() {
let name = String::from("Dylan");
let length = string_length(&name);
println!("{}", length); // 5
let mut name = name;
shout(&mut name);
println!("{}", name); // Dylan!
}References
fn string_length(input: &String) -> usize {
input.len()
}
fn shout(input: &mut String) {
input.push_str("!");
}
fn consume(input: String) {
println!("Nom nom nom {}", input);
}
fn main() {
let name = String::from("Dylan");
consume(name);
let length = string_length(&name); // Compilation error
println!("{}", length);
}Ownership rules
Result<T, E>
Option<T>
fn might_fail() -> Result<u8, String> {
// ...
}
enum Result<T, E> {
Ok(T),
Err(E),
}#[derive(Debug)]
enum MathError {
DivisionByZero,
NonPositiveLogarithm,
NegativeSquareRoot,
}
type MathResult = Result<f64, MathError>;
fn div(x: f64, y: f64) -> MathResult {
if y == 0.0 {
Err(MathError::DivisionByZero)
} else {
Ok(x / y)
}
}
fn speed(dist: f64, time: f64) -> MathResult {
let result = div(dist, time)?;
Ok(result)
}
fn main() {
println!("Speed: {}", speed(10.0, 4.0).unwrap());
}#[derive(Debug)]
enum MathError {
DivisionByZero,
NonPositiveLogarithm,
NegativeSquareRoot,
}
type MathResult = Result<f64, MathError>;#[derive(Debug)]
enum MathError {
DivisionByZero,
NonPositiveLogarithm,
NegativeSquareRoot,
}
type MathResult = Result<f64, MathError>;
fn div(x: f64, y: f64) -> MathResult {
if y == 0.0 {
Err(MathError::DivisionByZero)
} else {
Ok(x / y)
}
}type MathResult = Result<f64, MathError>;
fn div(x: f64, y: f64) -> MathResult {
// ...
}
fn speed(dist: f64, time: f64) -> MathResult {
let result: f64 = div(dist, time)?;
Ok(result)
}type MathResult = Result<f64, MathError>;
fn speed(dist: f64, time: f64) -> MathResult {
// ...
}
fn main() {
println!("Speed: {}", speed(10.0, 4.0).unwrap());
}struct Person {
first_name: String,
second_name: String,
address: Address,
}
Learn by doing...
struct Person {
first_name: String,
second_name: String,
address: Address,
}
struct Address {
number: StreetNumber,
street: String,
city: String,
}
struct Person {
first_name: String,
second_name: String,
address: Address,
}
struct Address {
number: StreetNumber,
street: String,
city: String,
}
enum StreetNumber {
House(u32),
Unit{unit: u32, street: u32}
}struct Person {
first_name: String,
second_name: String,
address: Address,
}
impl Person {
fn new(first_name: String, second_name: String, address: Address) -> Person {
Person {
first_name,
second_name,
address
}
}
fn full_name(&self) -> String {
format!("{} {}", self.first_name, self.second_name)
}
}trait Greet {
fn greet(&self);
}
struct Person {
first_name: String,
second_name: String,
address: Address,
}
impl Person {
fn full_name(&self) -> String {
format!("{} {}", self.first_name, self.second_name)
}
}
impl Greet for Person {
fn greet(&self) {
println!("Hello {}", self.full_name());
}
}
enum StreetNumber {
House(u32),
Unit{unit: u32, street: u32}
}
impl ToString for StreetNumber {
fn to_string(&self) -> String {
match self {
StreetNumber::House(num) => format!("{}", num),
StreetNumber::Unit{unit, street} => format!("{}/{}", unit, street)
}
}
}impl Person {
fn full_name(&self) -> String {
format!("{} {}", self.first_name, self.second_name)
}
}Declarative
Procedural
#[derive(Debug)]
struct Person {
first_name: String,
second_name: String,
address: Address,
}
#[derive(Debug)]
struct Address {
number: StreetNumber,
street: String,
city: String,
}
#[derive(Debug)]
enum StreetNumber {
House(u32),
Unit{unit: u32, street: u32}
}
curl https://sh.rustup.rs -sSf | shOnly ONE tool!
[package]
name = "bk-releases"
version = "0.1.0"
authors = ["Dylan Maccora <maccora17@gmail.com>"]
[dependencies]
reqwest = "0.8.*"
serde = "1"
serde_derive = "1"
serde_json = "1"
failure = "0.1"
url = "1.7"
clap = "2.32"
chrono = "0.4"Compiling playground v0.0.1 (file:///playground)
error[E0532]: expected tuple struct/variant, found struct variant `StreetNumber::Unit`
--> src/main.rs:58:13
|
58 | StreetNumber::Unit(unit, street) => format!("{}/{}", unit, street)
| ^^^^^^^^^^^^^^^^^^ did you mean `StreetNumber::Unit { /* fields */ }`?
error: aborting due to previous error
For more information about this error, try `rustc --explain E0532`.Cargo.toml
Or talk to me!