This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused)] | |
fn main() { | |
let x: Result<u32, &str> = Err("emergency failure"); | |
x.unwrap(); // panics with `emergency failure` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(dead_code)] | |
#[allow(non_snake_case)] | |
#[allow(non_camel_case_types)] | |
mod NumberFormat { | |
#[derive(Debug)] | |
pub enum Style { | |
DECIMAL, | |
CURRENCY, | |
PERCENT, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(dead_code)] | |
#[allow(non_snake_case)] | |
#[allow(non_camel_case_types)] | |
mod NumberFormat { | |
#[derive(Debug)] | |
pub enum Style { | |
DECIMAL, | |
CURRENCY, | |
PERCENT, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::mem::MaybeUninit; | |
const X: [MaybeUninit<u32>; 8] = [MaybeUninit::uninit(); 8]; | |
fn main() { | |
let _ = X[0]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt::Display; | |
fn print(obj: &dyn Display) { | |
println!("{}", obj); | |
} | |
fn main() { | |
print(&"test"); | |
print(&5); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code)] | |
use std::fmt::{self, Display}; | |
struct Spell { | |
classes: Vec<Class>, | |
name: String, | |
school: School, | |
level: u8, | |
casting_time: Time, |