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 March 14, 2018 01:12
Code shared from the Rust Playground
fn main() {
Box::new("qwe".into()) as Box<std::error::Error>;
}
@rust-play
rust-play / playground.rs
Created March 14, 2018 01:16
Code shared from the Rust Playground
fn main() {
let _ = Box::<std::error::Error>::from("oh no");
}
@rust-play
rust-play / playground.rs
Created March 14, 2018 01:17
Code shared from the Rust Playground
fn foo() -> Box<std::error::Error> {
"oh no".into()
}
fn main() {
let _ = foo();
}
@rust-play
rust-play / playground.rs
Created March 14, 2018 02:05
Code shared from the Rust Playground
#![feature(ptr_internals)]
use std::ptr::Unique;
fn main() {
let mut arr = [1u8, 2, 3];
let slice = &mut arr[..];
let unique : Unique<[u8]> = Unique::new(slice as *mut [u8]).unwrap();
// Cast unique to an usize
let ptr = unsafe { (&(*unique.as_ptr())[0] as *const u8 as usize) };
println!("{:?} {:x}", unique, ptr);
@rust-play
rust-play / playground.rs
Created March 14, 2018 02:35
Code shared from the Rust Playground
extern crate futures;
extern crate tokio_core;
use futures::executor::spawn;
use futures::future::{FutureResult, ok, Future};
use tokio_core::reactor::Core;
use std::fmt::Error;
fn main() {
let s = ok::<String, Error>(String::new())
@rust-play
rust-play / playground.rs
Created March 14, 2018 02:36
Code shared from the Rust Playground
use std::collections::HashMap;
use std::cell::RefCell;
fn main() {
let mut cache = HashMap::new();
cache.insert(0, 0);
let cache = RefCell::new(cache);
let x = if let Some(cached) = cache.borrow().get(&0).cloned() {
cached
} else {
@rust-play
rust-play / playground.rs
Created March 14, 2018 02:43
Code shared from the Rust Playground
#[feature(nll)]
use std::collections::HashMap;
use std::cell::RefCell;
fn main() {
let mut cache = HashMap::new();
cache.insert(0, 0);
let cache = RefCell::new(cache);
let x = if let Some(cached) = cache.borrow().get(&1).cloned() {
@rust-play
rust-play / playground.rs
Created March 14, 2018 02:51
Code shared from the Rust Playground
use std::collections::HashMap;
use std::cell::RefCell;
fn main() {
let mut cache = HashMap::new();
cache.insert(0, 0);
let cache = RefCell::new(cache);
let x = if let Some(cached) = {cache.borrow().get(&1).cloned()} {
cached
} else {
@rust-play
rust-play / playground.rs
Created March 14, 2018 02:53
Code shared from the Rust Playground
use std::collections::HashMap;
use std::cell::RefCell;
fn main() {
let mut cache = HashMap::new();
cache.insert(0, 0);
let cache = RefCell::new(cache);
let x = if let Some(cached) = {let x = cache.borrow().get(&1).cloned(); x} {
cached
} else {
@rust-play
rust-play / playground.rs
Created March 14, 2018 02:57
Code shared from the Rust Playground
fn main() {
let a = (1, 2, 3);
let mut b = 0;
println!("b: {} {:p}", b, &b);
let (x, b, y) = a;
println!("b: {} {:p}", b, &b);
}