Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jcar787's full-sized avatar

Jeff C. jcar787

View GitHub Profile
@jcar787
jcar787 / while-example.rs
Created April 8, 2022 18:55
While loop example in rust
fn main() {
let mut i = 1_u8;
while i < 8 {
println!("Your number is: {}", i + 5);
i += 1;
}
}
@jcar787
jcar787 / match-enum-example.rs
Created April 8, 2022 18:53
Match with Enum example in rust
enum StagesInLife {
Birth(String),
Grow,
Work(String),
Retirement { age: u8, pension: f32 }
}
fn main() {
let work = StagesInLife::Work("Principal Software Engineer".to_string());
match work {
@jcar787
jcar787 / match-example.rs
Created April 8, 2022 18:44
Example of match statement in rust
fn main() {
let number = 35;
match number {
5 => println!("FIVE!"),
10..=15 => println!("This is the correct answer"),
_ => println!("If you don't match anything go here")
}
}
@jcar787
jcar787 / if-example.rs
Created April 8, 2022 18:42
Example of if statements in rust
fn main() {
let i = 8;
if i > 5 {
println!("{} is greater than 5", i);
}
if i < 8 {
println!("This line is not going to run");
} else {
println!("But this does because its {}", i);
}
@jcar787
jcar787 / enum-example.rs
Created April 1, 2022 02:24
Enum example in Rust
enum StagesInLife {
Birth(String),
Grow,
Work(String),
Retirement { age: u8, pension: f32 }
}
fn main() {
let birth = StagesInLife::Birth("Jeff".to_string());
let grow = StagesInLife::Grow;
@jcar787
jcar787 / struct-example.rs
Created April 1, 2022 02:20
Struct example in Rust
struct Person {
name: String,
age: u8
}
fn main() {
let person = Person { name: "Jeff", age: 35 };
let name = String::from("Danny");
let age = 11;
let another_person = Person { name, person };
@jcar787
jcar787 / macro-vec.rs
Created April 1, 2022 02:18
vec! macro example
fn main() {
let mut another_vec =!vec[5, 1,] ; // another_vec = [5, 1]
another_vec.push(2); // another_vec = [5, 1, 2]
}
@jcar787
jcar787 / vectors.rs
Created April 1, 2022 02:16
Showing an example of Vectors
fn main() {
let mut my_vec: Vec<i32> = Vec::new(); // my_vec = []
my_vec.push(7); // my_vec = [7]
my_vec.push(8); // my_vec = [7, 8]
my_vec.push(7); // my_vec = [7, 8, 7]
}
@jcar787
jcar787 / mutable-array.rs
Created April 1, 2022 02:15
Showing an example of mutable arrays in Rust
fn main() {
let mut another_array: [i32:4] = [1,2,3,4];
another_array[0] = 7; // another_array = [7, 2, 3, 4];
}
@jcar787
jcar787 / scalars.rs
Created March 27, 2022 06:13
Primitive Types - Scalars
fn main() {
// The next 6 lines are immutable variables
let an_int = 35; // Default is i32
let another_int: u16 = 35_633;
let a_float: f32 = 2.56;
let another_float = 2.03_f64;
let my_bool = true;
let my_char = 'j';
println!("{}", an_int);