Skip to content

Instantly share code, notes, and snippets.

View jcar787's full-sized avatar

Jeff C. jcar787

View GitHub Profile
@jcar787
jcar787 / borrowing.rs
Created April 14, 2022 23:17
An example of borrowing in rust
fn give_me_your_name(name: &String) {
println!("Look at me, this is my name now! {}", name)
}
fn main() {
let my_name = String::from("Jeff");
give_me_your_name(&my_name);
 
println!("Not it is not, it still my name: {}", my_name); 
}
@jcar787
jcar787 / no-borrowing.rs
Created April 14, 2022 23:15
Losing ownership to a function (no borrowing) in rust
fn give_me_your_name(name: String) {
println!("Look at me, this is my name now! {}", name);
}
fn main() {
let my_name = String::from("Jeff");
give_me_your_name(my_name);
// Uncomment the next line and its gonna give you an error
// println!("This was my name: {}, my_name);
@jcar787
jcar787 / object-reference.js
Created April 14, 2022 23:12
JavaScript object reference
const myName = { name: "Jeff" };
const sameName = myName;
sameName.name += " C.";
console.log("My name is", myName.name);
@jcar787
jcar787 / string-ownership.rs
Created April 14, 2022 23:10
String ownership example
fn main() {
let my_name = String::from("Jeff");
let same_name = my_name;
println!("My name is {}", same_name);
// println!("My name is {}", my_name); // This is not going to work
}
@jcar787
jcar787 / string-example.rs
Created April 14, 2022 23:08
Simple example with String in rust
fn main() {
let my_name = String::from("Jeff");
println!("My name is {}", my_name);
}
@jcar787
jcar787 / fn-returns.rs
Created April 8, 2022 19:35
simple functions with returns in rust
fn another_sum(x: i32, y:i32) -> i32 {
x + y
}
fn sum(x:i32, y:i32) -> i32 {
return x + y;
}
fn main() {
println!("{}", sum(5, 8));
println!("{}", another_sum(9, 12));
@jcar787
jcar787 / fn-rust.rs
Created April 8, 2022 19:31
simple functions in rust
fn print_my_number(i: i32) {
println!("The value that you sent is: {}", i);
}
fn main() {
let i = 42;
print_my_number(i);
}
@jcar787
jcar787 / loop-loop.rs
Created April 8, 2022 19:03
Example of the loop loop in rust
fn main() {
let mut i = 0;
loop {
println!("Is this going to print for eternity?");
i += 1;
if i == 42 {
println!("Of course not. We have the answer");
break;
}
}
@jcar787
jcar787 / for-vector.rs
Created April 8, 2022 18:59
Using for with vectors in rust
fn main() {
let bands = vec!["Rush", "Black Sabbath", "Pink Floyd"];
for band in bands.iter() {
println!("{} is one of my favorite bands", band);
}
}
@jcar787
jcar787 / for-example.rs
Created April 8, 2022 18:57
For loop example in rust
fn main() {
for n in 1..8 {
println!("Your number is: {}", n + 5);
}
for n in 1..=8 {
println!("Your number is: {}", n + 5);
}
}