C | Rust
===========================================================
T value | value: T
T *value | value: *T
T* *value | value: **T
const T *value | value: *T
const T* const *value | value: **T
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
/** | |
* Because I have a large number of foriegn callback functions to wrap it'd be | |
* great I you could use a generic handle for local data setting and getting. | |
* Unfortunately when I try this I get: | |
* | |
* generic_tls.rs:23:64: 12:65 error: unexpected token: `,` | |
* generic_tls.rs:23 unsafe { task::local_data::local_data_set(tls_handle<FooFun>, cbfun); } | |
* | |
*/ |
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() { | |
let stuff = ~[~"one", ~"two", ~"three"]; | |
stuff | |
.map(|s| str::append(~"lol", *s)) | |
.map(|s| io::println(*s)); | |
} |
At the moment one of the limitations of pattern matching is the inability to use variables in the scope of the match.
pcwalton suggests using const
:
const origin: Point = Point { x: 0, y: 0 };
let foo = Point { x: 12, y: 42 }
match p {
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 to_str::ToStr; | |
macro_rules! map_cast( | |
(~[$($elems:expr),+] -> $T:ty) => (~[$($elems as $T),+]); | |
(@[$($elems:expr),+] -> $T:ty) => (@[$($elems as $T),+]); | |
($arr:expr -> $T:ty) => ($arr.map(|a| *a as $T)); | |
) | |
fn main() { | |
let arr1 = map_cast!(~[1, 2.5] -> ToStr); |
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
// example usage: | |
// | |
// check_gl!(glVertexAttribPointer(attrib, 4, GL_FLOAT, GL_FALSE as GLboolean, 0, null())); | |
// | |
// if there is an error it outputs: `OpenGL <<opengl error code>>: <<function identifier>>(<<function arguments>>)` | |
// | |
#[cfg(check_gl)] | |
macro_rules! check_gl ( |
Linenoise: I particularly love the 'completeLine' function, with its cute little 'beep' and graceful cascade of conditionals.
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
// | |
// let glfns = gl::init(); | |
// | |
// glfns.glViewport(0, 0, width as GLsizei, height as GLsizei); | |
// | |
struct GLProc { | |
glCullFace: extern fn(++mode: GLenum), | |
glFrontFace: extern fn(++mode: GLenum), |
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
extern mod std; | |
use std::time::precise_time_ns; | |
use cast::{reinterpret_cast, transmute}; | |
use ptr::to_unsafe_ptr; | |
pub struct Vec4 { x: float, y: float, z: float, w: float } | |
pub struct VecMatch { x: float, y: float, z: float, w: float } | |
pub impl VecMatch: Index<uint, float> { |
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
pure fn abs<T:Copy Num Ord>(x: &T) -> T { | |
if x >= from_int(0) { *x } else {-x } | |
} |