This file contains hidden or 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::hash::Hash; | |
use std::collections::{HashMap, HashSet}; | |
use std::slice; | |
pub type Value = u32; | |
// list of values, assumed to be small to large | |
pub static VALUES: [Value; 5] = [1, 2, 3, 4, 5]; | |
This file contains hidden or 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::thread; | |
use std::sync::Arc; | |
use std::rc::Rc; | |
use std::fmt::Debug ; | |
pub fn get_insertion_index<K: Ord + Debug + Clone>(values: &[K], k: &K) -> usize { | |
let mut index = 0; | |
for v in values{ | |
if *v >= *k { break;} | |
index += 1; |
This file contains hidden or 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() { | |
do_stuff((42, 0)); | |
do_stuff((0, 42)); | |
do_stuff((0, 0)); | |
do_stuff((123, 456)); | |
} | |
fn do_stuff(point: (u32, u32)) { | |
match point { |
This file contains hidden or 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
pub trait Ast<'a> { | |
fn evaluate(self: Box<Self>) -> Option<Box<Ast<'a> + 'a>>; | |
} | |
pub struct Assignment<'a> { | |
lhs: Option<Box<Ast<'a> + 'a>>, | |
rhs: Option<Box<Ast<'a> + 'a>> | |
} | |
impl<'a> Ast<'a> for Assignment<'a> { |
This file contains hidden or 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::rc::Rc; | |
use std::ops::Deref; | |
//A HasPosition trait and a Body that implements it | |
trait HasPosition { | |
fn get_position(&self) -> i32; | |
} | |
struct Body { | |
position: i32 |
This file contains hidden or 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::rc::Rc; | |
use std::ops::Deref; | |
//A HasPosition trait and a Body that implements it | |
trait HasPosition { | |
fn get_position(&self) -> i32; | |
} | |
struct Body { | |
position: i32 |
This file contains hidden or 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 get_c() -> Box<FnMut(i32)> { | |
Box::new(|val: i32| { | |
println!("value --> {}", val); | |
}) | |
} | |
pub fn set_c<F>(mut callback: F) | |
where F: FnMut(i32) | |
{ | |
callback(100); |
This file contains hidden or 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
struct Book { | |
name: String | |
} | |
impl Book { | |
fn show_book_name(&self) { | |
println!("Name is : {}", self.name); | |
} | |
} |
This file contains hidden or 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
// http://rosettacode.org/wiki/Define_a_primitive_data_type | |
/// Implements a custom type named CustomInt. | |
/// One problem that I'm not sure how to solve is bounds checking on variable assignments. | |
/// This type only implements a subset of all traits within std::ops. | |
use std::ops; | |
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)] | |
pub struct CustomInt { | |
value: u8, | |
} |
This file contains hidden or 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
// Define a structure named `List` containing a `Vec`. | |
use std::fmt; | |
use std::ops::Deref; | |
struct List(Vec<i32>); | |
impl Deref for List { | |
type Target = Vec<i32>; | |