Skip to content

Instantly share code, notes, and snippets.

@nick96
Last active August 10, 2021 13:41
Show Gist options
  • Save nick96/12944adb6fc10a19a4bad1b826f453f2 to your computer and use it in GitHub Desktop.
Save nick96/12944adb6fc10a19a4bad1b826f453f2 to your computer and use it in GitHub Desktop.
The well rounded rustation
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)]));
}
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]));
}
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2
}
};
println!("The result is {}", result);
}
fn big(n: i64) -> bool {
n > 100_000_000
}
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"));
}
fn main() {
let an_immutable_number = 5;
an_immutable_number += 5; // Won't compile!
let mut a_mutable_number = 10;
a_mutable_number += 5;
}
impl Plane for Jet {
fn fly() {
todo!()
}
fn land() {
todo!()
}
fn take_off() {
todo!()
}
}
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();
}
impl Jet {
fn eject() {
todo!();
}
}
#[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);
}
#[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);
}
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);
}
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
}
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
}
struct Jet {
wind: [Wing; 2],
engine: [Engine; 2],
}
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