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
fn main() { | |
apply_s(str::to_bytes("foo"), "foo", |buf, txt| { | |
buf == str::to_bytes(txt) | |
}); | |
apply_g(str::to_bytes("bar"), "bar", |buf, txt| { | |
buf == str::to_bytes(txt) | |
}); | |
} |
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
fn gen_to_str<T: Num Zero One Ord Round ToStr>(num: T) -> ~str { | |
let mut s = ~""; | |
let mut accum = num; | |
let _base: T = Num::from_int(10); | |
while(accum > Zero::zero()) { | |
let digit: T = accum % _base; | |
accum /= _base; | |
accum = accum.floor(); | |
s = digit.to_str() + s; |
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
Without macros: | |
pub fn from_bytes_common<T: Num Zero One Copy>(buf: &[u8], radix: uint, | |
fractional: bool, exponent: ExponentFormat, | |
special: bool, empty_zero: bool) -> Option<T> { | |
match exponent { | |
ExpDec if radix > DIGIT_E | |
=> fail fmt!("from_bytes_common: radix %? incompatible with decimal exponent format", radix), | |
ExpBin if radix > DIGIT_P | |
=> fail fmt!("from_bytes_common: radix %? incompatible with binary exponent format", radix), | |
_ if special && radix > DIGIT_I // lowest first digit in "inf" and "NaN" |
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
fn foo(a: Option<int>, b: Option<int>, c: Option<int>) { | |
arg_fail!( in "foo" | |
if matches!(a: None) : "a missing"; | |
if matches!(b: Some(_)) : "Don't need b"; | |
if matches!(b: None) && matches!(c: None) : "Need neither b nor c"; | |
) | |
... | |
} |
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 core::num::Num::*; | |
fn main() { | |
let x: float = Num::from_int(7); | |
} | |
----------------------------------------------------------------- | |
outputs: | |
test-prelude.rs:4:19: 4:32 error: unresolved name | |
test-prelude.rs:4 let x: float = Num::from_int(7); |
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
#[path="math/round.rs"] | |
mod round; | |
mod numgenerics; | |
//use core::Num; | |
//use core::num; | |
//use core::num::*; | |
//use core::prelude::*; | |
// Todo: cleanum imports in code. |
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
Author: Graydon Hoare <graydon@mozilla.com> | |
Date: Wed Jan 9 10:43:16 2013 -0800 | |
rustc: more arch-specific fallout from 2db3abd |
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
type T = int; | |
priv fn ioprintnum(num: T) { | |
let mut buf: ~[u8] = ~[]; | |
let mut deccum = if num < 0 as T {-num} else {num}; | |
loop { | |
let d = deccum % 10 as T; | |
deccum /= 10 as T; | |
buf.push(match d { | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 => '0' as u8 + d as u8, |
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
trait ConvTo<T> { | |
fn to(&self) -> T; | |
} | |
impl i8: ConvTo<u8> { | |
fn to(&self) -> u8 { *self as u8 } | |
} | |
impl i8: ConvTo<u16> { | |
fn to(&self) -> u16 { *self as u16 } |
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
trait Integer {} | |
impl Integer for int {} | |
trait Float { static fn NaN() -> Self; } | |
impl Float for float { static fn NaN() -> float { 0./0. } } | |
trait NumStrHelper { | |
static fn has_special_values() -> bool; | |
static fn NaN() -> Option<Self>; | |
fn is_NaN(&self) -> bool; |
OlderNewer