Skip to content

Instantly share code, notes, and snippets.

View ZhangHanDong's full-sized avatar
🦀
Rustacean

Alex ZhangHanDong

🦀
Rustacean
  • Beijing, China
View GitHub Profile
@ZhangHanDong
ZhangHanDong / TypeArithmetic.rs
Created March 19, 2021 14:39 — forked from ddfisher/TypeArithmetic.rs
Type-level Arithmetic in Rust
#![feature(core_intrinsics)]
// This requires unstable features to compile so we can print out the type names. The type
// arithmetic itself works fine on stable.
use std::marker::PhantomData;
enum Zero {}
enum Succ<T> {
Succ(PhantomData<T>), // only required to satisfy the compiler

Follow-up to Method on Emulating Higher-Kinded Types (HKTs) in Rust

First off, thanks for all the comments and kind words on the original writeup; I've been meaning to follow up on some of the suggestions and write about the different ways to represent monads (and functors, HKTs, etc) that now exist, but a month of being busy has kind of gotten in the way (mainly with three new kittens!).

And for sure, I do not expect (nor do I want) this to become the norm for production-level Rust: rather, I hope that this can contribute to the foundations of programming with higher-level abstractions in Rust, somewhat like how early template metaprogramming in C++ and typeclass-constraint-unification metaprogramming in Haskell have contributed, perhaps indirectly, to later innovations in their respective languages and ecosystems that were much more reasoned, sound and usable.

Changes, Edits, Refinements

One of the things suggested in the com

Rust in Large Organizations

Initially taken by Niko Matsakis and lightly edited by Ryan Levick

Agenda

  • Introductions
  • Cargo inside large build systems
  • FFI
  • Foundations and financial support
@ZhangHanDong
ZhangHanDong / string-conversion.rs
Created October 12, 2020 22:08 — forked from jimmychu0807/string-conversion.rs
Conversion between String, str, Vec<u8>, Vec<char> in Rust
use std::str;
fn main() {
// -- FROM: vec of chars --
let src1: Vec<char> = vec!['j','{','"','i','m','m','y','"','}'];
// to String
let string1: String = src1.iter().collect::<String>();
// to str
let str1: &str = &src1.iter().collect::<String>();
// to vec of byte
@ZhangHanDong
ZhangHanDong / playground.rs
Created September 24, 2020 21:36 — forked from anonymous/playground.rs
Rust code shared from the playground
impl<
'four>For for
& 'four for<
'fore>For where
for<'fore>For:For
{fn four( self:&&
'four for <'fore>
For){ print!(
"four" )}} fn
main(){ four(&(
@ZhangHanDong
ZhangHanDong / fizzbuzz.rs
Created August 21, 2020 14:07 — forked from mre/fizzbuzz.rs
Fizzbuzz in Rust
#![feature(generators)]
#![feature(generator_trait)]
use std::ops::{Generator, GeneratorState};
fn fizzbuzz_println(upto: u64) {
for i in 0..upto {
if i % 3 == 0 && i % 5 == 0 {
println!("FizzBuzz");
} else if i % 3 == 0 {
#![warn(rust_2018_idioms)]
#[derive(Debug)]
pub struct StrSplit<'haystack, D> {
remainder: Option<&'haystack str>,
delimiter: D,
}
impl<'haystack, D> StrSplit<'haystack, D> {
pub fn new(haystack: &'haystack str, delimiter: D) -> Self {
@ZhangHanDong
ZhangHanDong / design_is_import.rs
Created July 19, 2020 10:49 — forked from rust-play/playground.rs
Code shared from the Rust Playground
pub struct Monster {
hp: u8, // health points
sp: u8, // spell points
friends: Vec<Friend>,
}
pub struct Friend {
loyalty: u8,
}
@ZhangHanDong
ZhangHanDong / immutable_and_mutable_tip.rs
Last active July 19, 2020 10:22 — forked from rust-play/playground.rs
Code shared from the Rust Playground
pub struct Monster {
hp: u8, // health points
sp: u8, // spell points
friends: Vec<Friend>,
}
pub struct Friend {
loyalty: u8,
}
@ZhangHanDong
ZhangHanDong / trait_and_trait_object.rs
Last active July 18, 2020 11:58 — forked from rust-play/playground.rs
Code shared from the Rust Playground
trait A{
fn a(&self);
}
trait B{
fn b(&self);
}
impl B for A{