Last active
August 10, 2021 13:41
-
-
Save nick96/12944adb6fc10a19a4bad1b826f453f2 to your computer and use it in GitHub Desktop.
The well rounded rustation
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 count(xs: Vec<Box<dyn std::any::Any>>) -> usize { | |
let mut n = 0; | |
for _ in xs { | |
n += 1; | |
} | |
n | |
} | |
fn main() { | |
println!("str: {}", count(vec![Box::new("1"), Box::new("2"), Box::new(1), Box::new(2)])); | |
} |
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 count(xs: Vec<&dyn std::any::Any>) -> usize { | |
let mut n = 0; | |
for _ in xs { | |
n += 1; | |
} | |
n | |
} | |
fn main() { | |
println!("str: {}", count(vec![&"1", &"2", &1, &2])); | |
} |
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() { | |
let mut counter = 0; | |
let result = loop { | |
counter += 1; | |
if counter == 10 { | |
break counter * 2 | |
} | |
}; | |
println!("The result is {}", result); | |
} |
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 big(n: i64) -> bool { | |
n > 100_000_000 | |
} |
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 count<T>(xs: Vec<T>) -> usize { | |
let mut n = 0; | |
for _ in xs { | |
n += 1; | |
} | |
} | |
fn main() { | |
println!("int: {}", count(vec![1, 2, 3])); | |
println!("str: {}", count(vec!["1", "2", "3")); | |
} |
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() { | |
let an_immutable_number = 5; | |
an_immutable_number += 5; // Won't compile! | |
let mut a_mutable_number = 10; | |
a_mutable_number += 5; | |
} |
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
impl Plane for Jet { | |
fn fly() { | |
todo!() | |
} | |
fn land() { | |
todo!() | |
} | |
fn take_off() { | |
todo!() | |
} | |
} |
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
use std::thread; | |
#[derive(Debug)] | |
struct MyBox(*mut u8); | |
unsafe impl Send for MyBox {} | |
fn main() { | |
let mut x = 5; | |
let test = MyBox(&mut x); | |
let handle = thread::spawn(move || { | |
println!("test={:?}", test); | |
}); | |
handle.join().unwrap(); | |
} |
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
impl Jet { | |
fn eject() { | |
todo!(); | |
} | |
} |
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
#[derive(Debug)] | |
struct Thing { | |
this: i32, | |
} | |
fn mutable_var(mut thing: &Thing) { | |
println!("mutable_var (before): {:?}", thing); | |
thing = &Thing { this: 5 }; | |
println!("mutable_var (after): {:?}", thing); | |
} | |
fn main() { | |
let thing = Thing { this: 10 }; | |
println!("main (before): {:?}", thing); | |
mutable_var(&thing); | |
println!("main (after): {:?}", thing); | |
} |
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
#[derive(Debug)] | |
struct Thing { | |
this: i32, | |
} | |
fn mutable_var(thing: &mut Thing) { | |
println!("mutable_var (before): {:?}", thing); | |
thing.this = 5; | |
println!("mutable_var (after): {:?}", thing); | |
} | |
fn main() { | |
let mut thing = Thing { this: 10 }; | |
println!("main (before): {:?}", thing); | |
mutable_var(&mut thing); | |
println!("main (after): {:?}", thing); | |
} |
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() { | |
let mut s = "hello".to_string(); | |
let r = &mut s; | |
r.push_str(" world"); | |
(*r).push_str(" today"); | |
println!("r is: {}", r); | |
println!("&r is: {}", &r); | |
println!("*r is: {}", *r); | |
println!("s is: {}", s); | |
println!("&s is: {}", &s); | |
} |
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 ret_ref<'a>(s: &'a str, on: char) -> Vec<&'a str> { | |
let mut parts = vec![]; | |
let mut start = 0; | |
for (i, c) in s.chars().enumerate() { | |
if c == on { | |
parts.push(&s[start..i]); | |
start = i + 1; | |
} | |
} | |
parts.push(&s[start..]); | |
parts | |
} |
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 ret_val(s: &str, on: char) -> Vec<String> { | |
let mut xs = vec![]; | |
let mut part = "".to_string(); | |
for c in s.chars() { | |
if c == on { | |
xs.push(part); | |
part = "".to_string(); | |
} else { | |
part.push(c); | |
} | |
} | |
xs.push(part); | |
xs | |
} |
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
struct Jet { | |
wind: [Wing; 2], | |
engine: [Engine; 2], | |
} |
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
trait Plane { | |
fn fly(); | |
fn land(); | |
fn take_off(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment