Created
June 9, 2022 18:03
-
-
Save encody/35b349c0d4474a4717c26c3f626d0531 to your computer and use it in GitHub Desktop.
Introduction to Rust Workshop @near HH ATX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
// Printing to the screen | |
println!("Hello, world!"); | |
// Variables & mutability | |
let mut x = 1; | |
x = 4; | |
// Arrays, vectors, ranges, and loops | |
let x = 5; | |
println!("My variable: {}", x); | |
let mut my_arr = vec![1, 2, 3, 4]; | |
my_arr.push(5); | |
let mut explicit_vec = Vec::new(); | |
explicit_vec.push(1); | |
explicit_vec.push(2); | |
explicit_vec.push(3); | |
explicit_vec.push(4); | |
println!("My vector: {:?}", my_arr); | |
for i in explicit_vec { | |
println!("{i}"); | |
} | |
let mut i = 0; | |
while i < explicit_vec.len() { | |
println!("{}", explicit_vec[i]); | |
i += 1; | |
} | |
let mut i = 0; | |
loop { | |
if i >= explicit_vec.len() { | |
break; | |
} | |
println!("{}", explicit_vec[i]); | |
i += 1; | |
} | |
let x = "string"; | |
println!("x: {x}"); | |
// Control flow | |
let my_string = "ing"; | |
let my_value = if my_string.len() > 5 { | |
"very long" | |
} else { | |
"not very long" | |
}; | |
println!("my value is: {my_value}"); | |
// Functions | |
// Functions with owned vs. borrowed arguments | |
fn say_hello(name: &String) { | |
println!("hello, {name}!"); | |
} | |
let my_name = "Jacob".to_string(); | |
say_hello(&my_name); | |
say_hello(&my_name); | |
say_hello(&my_name); | |
say_hello(&my_name); | |
// Lifetimes | |
fn longest<'a>(first: &'a String, second: &'a String) -> &'a String { | |
if first.len() > second.len() { | |
first | |
} else { | |
second | |
} | |
} | |
let long_string = "very long string".to_string(); // begin: 'a | |
let mut answer = "".to_string(); | |
{ | |
let the_longest_string = "sasdf".to_string(); // begin: 'b | |
let lives_short = longest(&long_string, &the_longest_string); | |
answer = lives_short.to_owned(); | |
} // end: 'b | |
println!("answer: {}", answer); | |
// Structs and enums | |
#[derive(Debug)] | |
struct TupleStruct(pub i32, pub String); | |
let my_tuple_struct = TupleStruct(4, "hello".to_string()); | |
println!("{:?}", my_tuple_struct); | |
#[derive(Debug)] | |
struct MyStruct { | |
pub number: i32, | |
pub string: String, | |
pub tuple_struct: TupleStruct, | |
} | |
let instance = MyStruct { | |
number: 4, | |
string: "hello".to_string(), | |
tuple_struct: my_tuple_struct, | |
}; | |
println!("{:?}", instance); | |
pub enum MyOption<T> { | |
None, | |
Some(T), | |
} | |
fn might_return_nothing(x: i32) -> Option<i32> { | |
if x == 0 { | |
None | |
} else { | |
Some(500 / x) | |
} | |
} | |
let answer = might_return_nothing(0); | |
match answer { | |
None => println!("Cannot divide by zero"), | |
Some(result) => println!("The answer is: {result}"), | |
} | |
pub enum Result<T, E> { | |
Ok(T), | |
Err(E), | |
} | |
let x = Result::<&str, ()>::Err(()); | |
match x { | |
Ok(s) => println!("string: {s}"), | |
Err(()) => println!("error"), | |
} | |
let env_var = std::env::var("MY_VAR").ok(); | |
println!("{:?}", env_var); | |
let mut my_book = Book { | |
title: "The Portrait of Dorian Gray".to_string(), | |
author: "Oscar Wilde".to_string(), | |
condition: Condition::New, | |
}; | |
println!("{my_book:?}"); | |
my_book.damage_in_place(); | |
println!("{my_book:?}"); | |
let damaged_copy = my_book.damage_copy(); | |
println!("{damaged_copy:?}"); | |
} // end: 'a | |
#[derive(Debug)] | |
enum Condition { | |
New, | |
Good, | |
Fair, | |
Poor, | |
} | |
#[derive(Debug)] | |
struct Book { | |
pub title: String, | |
pub author: String, | |
pub condition: Condition, | |
} | |
impl Book { | |
pub fn damage_copy(&self) -> Book { | |
Book { | |
title: self.title.clone(), | |
author: self.author.clone(), | |
condition: match self.condition { | |
Condition::New => Condition::Good, | |
Condition::Good => Condition::Fair, | |
_ => Condition::Poor, | |
}, | |
} | |
} | |
pub fn damage_in_place(&mut self) { | |
self.condition = match self.condition { | |
Condition::New => Condition::Good, | |
Condition::Good => Condition::Fair, | |
_ => Condition::Poor, | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment