Skip to content

Instantly share code, notes, and snippets.

View rust-play's full-sized avatar

The Rust Playground rust-play

View GitHub Profile
@rust-play
rust-play / playground.rs
Created January 24, 2020 16:00
Code shared from the Rust Playground
use std::ops::Index;
struct Foo<T> {
val: T,
}
impl<T> Index<usize> for (&Foo<T>, &str) {
type Output = T;
fn index(&self, _: usize) -> &T {
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:47
Code shared from the Rust Playground
use std::convert::Infallible;
use std::net::SocketAddr;
use hyper::{Body, Request, Response, Server, StatusCode};
use hyper::service::{make_service_fn, service_fn};
#[tokio::main]
async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 3030));
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:46
Code shared from the Rust Playground
use std::io::{self, BufRead};
struct Migration<'a> {
pub namespace: &'a str,
}
impl<'a> Migration<'a> {
pub fn len(&self) -> usize {
self.namespace.chars().count()
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:42
Code shared from the Rust Playground
extern crate futures;
extern crate reqwest;
extern crate scraper;
use futures::{future, Future};
use scraper::{Html, Selector};
use std::time::Instant;
async fn get_doc(url: &str) -> Result<String, Box<dyn std::error::Error>> {
let doc = reqwest::get(url).await?.text().await?;
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:42
Code shared from the Rust Playground
extern crate futures;
extern crate reqwest;
extern crate scraper;
use futures::{future, Future};
use scraper::{Html, Selector};
use std::time::Instant;
async fn get_doc(url: &str) -> Result<String, Box<dyn std::error::Error>> {
let doc = reqwest::get(url).await?.text().await?;
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:37
Code shared from the Rust Playground
use std::mem::ManuallyDrop;
use std::convert::TryInto;
fn main() {
let mut a = String::with_capacity(50);
a.push_str("öäasdkflieojf");
let (b,c) = unsafe{split(a, 4)};
println!("{} and {}, capacities: {}, {}", b, c, b.capacity(), c.capacity());
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:35
Code shared from the Rust Playground
struct Int32(i32);
struct Int64(i64);
pub trait Num {
fn new() -> Self;
}
impl Num for Int32 {
fn new() -> Self { Int32(0) }
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:32
Code shared from the Rust Playground
use std::cmp::max;
use rayon::prelude::*;
fn max_k(n: u64) -> u64 {
let p = n as f64;
return ((((8.0f64*p)+1.0f64).sqrt()-1.0f64)/2.0f64) as u64;
}
fn f(n: u64, k: &u64) -> u64 {
let total = (k*(k+1))/2;
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:27
Code shared from the Rust Playground
struct Int32(i32);
struct Int64(i64);
pub trait Num {
fn new() -> Self;
}
impl Num for Int32 {
fn new() -> Self { Int32(0) }
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 15:26
Code shared from the Rust Playground
#![allow(unused)]
fn main() {
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let five = 5;
///
/// assert_eq!(6, my_crate::add_one(5));