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
| let board = [Empty, ..9]; | |
| let line = [0, 3, 6]; | |
| let get = line.iter().map(|&i| board[i]); | |
| for p in get { | |
| println!("{:?}", p); | |
| } |
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
| let win_circle = [Circle, ..3]; | |
| let win_cross = [Cross, ..3]; | |
| let test = [Empty, Cross, Circle]; | |
| match test { | |
| win_circle => println!("Circle wins!"), | |
| win_cross => println!("Cross wins!"), | |
| _ => println!("Game continues") | |
| } |
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 play(circles_move: |TicTacToe| -> TicTacToe, | |
| crosses_move: |TicTacToe| -> TicTacToe) { | |
| let mut game = tictactoe::TicTacToe::empty_board(); | |
| game.print(); | |
| while !game.is_game_over() { | |
| match game.player() { | |
| PlayerCircle => { | |
| println!("Circles move: "); | |
| game = circles_move(game); | |
| } |
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
| extern crate collections; | |
| use collections::HashMap; | |
| use std::hash::Hash; | |
| fn main() { | |
| let mut map: CountedSet<&'static str> = CountedSet::new(); | |
| let strings = ["this", "is", "a", "list", "of", "of", "strings"]; | |
| for k in strings.iter() { | |
| map.add(*k); | |
| } |
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 make_human_move1(game: TicTacToe) -> TicTacToe { | |
| let mut move = parse_move_from_stdin(); | |
| let new_game = game.make_move_from_input(move); | |
| loop { | |
| match new_game { | |
| Some(g) => return g, | |
| None => { | |
| println!("Bad input / invalid move, try again:"); | |
| move = parse_move_from_stdin(); | |
| let new_game = game.make_move_from_input(move); |
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
| extern crate collections; | |
| use collections::HashMap; | |
| use std::hash::Hash; | |
| fn main() { | |
| let mut map: CountedSet<&'static str> = CountedSet::new(); | |
| let strings = ["a", "b", "a", "b", "b", "c"]; | |
| let answer = [("b", 3u), ("a", 2), ("c", 1)]; | |
| for k in strings.iter() { | |
| map.add(*k); |
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 subtract(&mut self, mut other: &CountedSet<T>) { | |
| for (key, count) in other.iter() { | |
| // Data is a HashMap<T, uint> | |
| match self.data.find_copy(key) { | |
| Some(c) => { self.data.insert(*key, c - *count); } | |
| _ => {} | |
| } | |
| } | |
| } |
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() { | |
| // CountedSet is HashMap<uint, uint>, with the counts of each key | |
| let numbers = [1u, 1, 1, 2, 2, 3, 1]; | |
| let more_numbers = [1u, 1, 2]; | |
| let count1: CountedSet<uint> = numbers.iter().map(|&x|x).collect(); | |
| let count2: CountedSet<uint> = more_numbers.iter().map(|&x|x).collect(); | |
| println!("{}", count1); | |
| println!("{}", count2); | |
| for (o, e) in Repeat::new(&count1.iter()).zip(count2.iter()) { | |
| println!("{:?} ---- {:?}", o, e); |
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
| #[deriving(Clone)] | |
| struct CountedSet<T> { | |
| data: HashMap<T, uint> | |
| } | |
| fn subtract(&self, other: &CountedSet<T>) -> CountedSet<T> { | |
| let mut result = self.clone(); | |
| for (key, count) in other.iter() { | |
| match result.data.find_copy(key) { | |
| Some(old) => { |
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
| extern crate collections; | |
| use collections::HashMap; | |
| fn main () { | |
| } | |
| static MISSING: uint = 0; | |
| #[deriving(Clone)] |
OlderNewer