This file contains hidden or 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
const std = @import("std"); | |
pub const Trait = struct { | |
ptr: *anyopaque, | |
vtable: Vtable, | |
const Vtable = struct { | |
hello: *const fn (*anyopaque) void, | |
}; | |
}; |
This file contains hidden or 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::{marker::PhantomData, ptr::NonNull}; | |
trait Foo { | |
fn foo(&self); | |
} | |
struct Anydata<'a> { | |
_p: PhantomData<&'a ()>, | |
data: NonNull<()>, | |
fns: &'static Anyfn, |
This file contains hidden or 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() { | |
// 2d array | |
let mut set = [ | |
[0, 0, 7, 5, 0, 0, 0, 0, 0], | |
[1, 0, 5, 2, 0, 0, 0, 0, 0], | |
[0, 0, 0, 1, 3, 0, 0, 9, 0], | |
[7, 0, 0, 0, 0, 0, 0, 8, 0], | |
[3, 0, 0, 0, 0, 0, 0, 4, 0], | |
[2, 5, 0, 0, 0, 6, 0, 0, 0], | |
[0, 0, 0, 1, 0, 0, 0, 0, 4], |
This file contains hidden or 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::env; | |
fn main() { | |
if let Some(s) = env::args().nth(1) { | |
let x = palfinder(&s); | |
println!("For {}:", s); | |
x.into_iter().for_each(|x| println!("{}", x)); | |
} |
This file contains hidden or 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 x = Hi(String::from("HELLO!")); | |
let y = x.0.as_bytes() as *const [u8] as *mut [u8]; | |
unsafe { | |
(*y)[0] = "X".as_bytes()[0]; | |
} | |
println!("{:?}", x); | |
} |
This file contains hidden or 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
pub trait All { | |
fn print(&self); | |
} | |
pub struct Dynamic<'a, const SIZE: usize>(pub [&'a dyn All; SIZE]); | |
impl All for u8 { | |
fn print(&self) { | |
println!("{}", self); | |
} |
This file contains hidden or 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
// Defining a struct called Test with a default generic param | |
struct Test<A, B = Sample> { | |
a: A, | |
b: B, | |
} | |
struct Sample { | |
one: u8, | |
} |
This file contains hidden or 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::cell::UnsafeCell; | |
use std::sync::atomic::{AtomicI32, Ordering}; | |
use std::thread; | |
use std::time::Duration; | |
struct Mutex<T> { | |
data: UnsafeCell<T>, | |
} | |
impl<T> Mutex<T> { |
This file contains hidden or 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
// Lifetime is just regions of code | |
/* | |
Argument s is invariant over &'b str, can only be mutated with the same lifetime | |
Argument delimiter is by-value | |
*/ | |
pub fn strtok<'a, 'b: 'a>(s: &'a mut &'b str, delimiter: char) -> &'b str { | |
if let Some(i) = s.find(delimiter) { | |
let prefix = &s[..i]; | |
let suffix = &s[(i + 1)..]; | |
*s = suffix; // suffix and s has the same lifetime |
This file contains hidden or 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::mem::MaybeUninit; | |
/* | |
Why do something like this? | |
To save a few CPU cycles and initializing to a value that will be overwritten. | |
*/ | |
// This is wrapped in unsafe because you can do something like | |
// initializer(123456 as *const string); 123456 being some memory address. | |
unsafe fn initializer(uninit: *mut String) { |
NewerOlder