Skip to content

Instantly share code, notes, and snippets.

The Rust Playground rust-play

View GitHub Profile
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
// https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters
// Note this is a subscribe problem
// Good morning! Here's your coding interview problem for today.
//
// This problem was asked by Amazon.
//
// Given an integer k and a string s, find the length of the longest substring that contains at most k distinct characters.
//
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
//Is Unique: Implement an algorithm to determine if a string has all unique characters.
//What if you cannot use additional data structures?
use std::collections::HashMap;
fn main() {
//println!("Hello World!");
println!("{}", has_all_unique("asdasd"));
println!("{}", has_all_unique_hash("asdasd"));
}
fn has_all_unique(word: &str) -> bool {
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
pub trait Trait<T: std::fmt::Debug> {
fn display(&self, t: T) {
println!("{:?}", t);
}
}
pub struct Dummy;
impl<T: std::fmt::Debug> Trait<T> for Dummy{}
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
//Is Unique: Implement an algorithm to determine if a string has all unique characters.
//What if you cannot use additional data structures?
use std::collections::HashMap;
fn main() {
//println!("Hello World!");
println!("{}", has_all_unique("asdasd"));
println!("{}", has_all_unique_hash("asdasd"));
}
fn has_all_unique(word: &str) -> bool {
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
trait Zoo {
type X;
}
impl Zoo for u16 {
type X = usize;
}
fn foo(abc: <u16 as Zoo>::X) {}
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
type ParseResult<'a, Output> = Result<(&'a str, Output), &'a str>;
pub trait Parser<'a, Output> {
fn parse(&self, input: &'a str) -> ParseResult<'a, Output>;
fn map<F, NewOutput>(self, map_fn: F) -> BoxedParser<'a, NewOutput>
where
F: Fn(Output) -> NewOutput + 'a,
Output: 'a,
NewOutput: 'a,
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
fn foo(iter: impl Iterator) -> impl Iterator {
let closure = |x| x;
if 0 == 0 {
iter.map(closure)
} else {
iter.map(closure)
}
}
fn main() {
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
fn foo(iter: impl Iterator) -> impl Iterator {
if 0 == 0 {
iter.map(|x| x)
} else {
iter.filter(|x| true)
}
}
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
#![feature(try_blocks)]
fn main() {
use std::num::ParseIntError;
let result: Result<i32, ParseIntError> = try {
"1".parse::<i32>()?
+ "2".parse::<i32>()?
+ "3".parse::<i32>()?
};
@rust-play
rust-play / playground.rs
Created Apr 20, 2019
Code shared from the Rust Playground
View playground.rs
extern fn foo() {}
extern "C" {
// This doesn't cast `Some(foo)` to the correct type
fn printf(fmt: *const u8, ...);
// This works correctly
//fn printf(fmt: *const u8, _: Option<extern fn()>);
}
You can’t perform that action at this time.