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 13:09
Code shared from the Rust Playground
#![allow(unused)]
fn main() {
let x: Result<u32, &str> = Err("emergency failure");
x.unwrap(); // panics with `emergency failure`
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 13:06
Code shared from the Rust Playground
#[allow(dead_code)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
mod NumberFormat {
#[derive(Debug)]
pub enum Style {
DECIMAL,
CURRENCY,
PERCENT,
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 13:02
Code shared from the Rust Playground
use std::io::{Result, BufRead};
fn read_until<R: BufRead>(mut read: R, out: &mut Vec<u8>, pair: (u8, u8)) -> Result<usize> {
let mut bytes_read = 0;
let mut got_possible_terminator = false;
loop {
let buf = read.fill_buf()?;
if buf.len() == 0 { return Ok(bytes_read); } // EOF
@rust-play
rust-play / playground.rs
Created January 24, 2020 13:00
Code shared from the Rust Playground
#[allow(dead_code)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
mod NumberFormat {
#[derive(Debug)]
pub enum Style {
DECIMAL,
CURRENCY,
PERCENT,
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 12:43
Code shared from the Rust Playground
/// Contant time implementation of u32::pow_overflowing
///
/// This function uses a regular right-to-left double-and-add algorithm.
pub fn ct_pow_overflowing_u32(mut n: u32, mut exp: u32) -> (u32, bool) {
let mut overflow_in_acc = false;
let mut overflow_in_n = false;
let mut acc = 1u32;
for _ in 0..8 * core::mem::size_of::<u32>() {
let mask = -((exp & 1) as i32) as u32;
let tmp = (n & mask) | (1 & !mask);
@rust-play
rust-play / playground.rs
Created January 24, 2020 12:30
Code shared from the Rust Playground
use std::mem::MaybeUninit;
const X: [MaybeUninit<u32>; 8] = [MaybeUninit::uninit(); 8];
fn main() {
let _ = X[0];
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 12:23
Code shared from the Rust Playground
// An example to illustrate that 'mut' in front of a parameter name
// is NOT part of the signature of the function.
pub struct Point {
x: f64,
y: f64,
}
pub trait Translate: Sized {
fn translate(&mut self, dx: f64, dy: f64);
@rust-play
rust-play / playground.rs
Created January 24, 2020 12:17
Code shared from the Rust Playground
// An example to illustrate that 'mut' in front of a parameter name
// is NOT part of the signature of the function.
pub struct Point {
x: f64,
y: f64,
}
pub trait Translate {
fn translate(self, dx: f64, dy: f64) -> Self;
@rust-play
rust-play / playground.rs
Created January 24, 2020 12:07
Code shared from the Rust Playground
use std::fmt::Display;
fn print(obj: &dyn Display) {
println!("{}", obj);
}
fn main() {
print(&"test");
print(&5);
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 11:50
Code shared from the Rust Playground
#![allow(dead_code)]
use std::fmt::{self, Display};
struct Spell {
classes: Vec<Class>,
name: String,
school: School,
level: u8,
casting_time: Time,