Last active
December 29, 2015 22:49
-
-
Save mythmon/7739008 to your computer and use it in GitHub Desktop.
I'm having trouble with rust borrowed pointers
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
$ rust run tetrissums.rs | |
grid.rs:9:27: 9:32 error: Illegal anonymous lifetime: anonymous lifetimes are not permitted here | |
grid.rs:9 highlight: Option<&'self Piece>, | |
^~~~~ | |
pieces.rs:4:15: 4:19 error: Illegal anonymous lifetime: anonymous lifetimes are not permitted here | |
pieces.rs:4 grid: &'self Grid, |
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; | |
use std::fmt; | |
use std::rand::random; | |
use pieces::Piece; | |
pub struct Grid<'self> { | |
data: ~[~[uint]], | |
highlight: Option<&'self Piece>, | |
} | |
impl<'self> Grid<'self> { | |
pub fn zero(rows: uint, cols: uint) -> Grid { | |
let mut data = ~[]; | |
for x in range(0, cols) { | |
data.push(~[]); | |
for _ in range(0, rows) { | |
data[x].push(0); | |
} | |
} | |
Grid { | |
data: data, | |
highlight: None, | |
} | |
} | |
pub fn random(rows: uint, cols: uint) -> Grid { | |
let mut data = ~[]; | |
for x in range(0, cols) { | |
data.push(~[]); | |
for _ in range(0, rows) { | |
let r = random::<uint>() % 99 + 1; | |
data[x].push(r); | |
} | |
} | |
Grid { | |
data: data, | |
highlight: None, | |
} | |
} | |
} | |
impl<'self> std::to_str::ToStr for Grid<'self> { | |
fn to_str(&self) -> ~str { | |
format!("{}", *self) | |
} | |
} | |
impl<'self> fmt::Default for Grid<'self> { | |
fn fmt(g: &Grid, fmt: &mut fmt::Formatter) { | |
for row in g.data.iter() { | |
for cell in row.iter() { | |
fmt.buf.write(format!("{:0>2u}", *cell).into_bytes()); | |
fmt.buf.write(" ".as_bytes()); | |
} | |
fmt.buf.write("\n".as_bytes()); | |
} | |
} | |
} |
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 grid::Grid; | |
pub struct Piece<'self> { | |
grid: &'self Grid, | |
shape: &'self Shape, | |
position: Point, | |
} | |
impl<'self> Piece<'self> { | |
pub fn sum(&self) -> uint { | |
let mut s = 0; | |
for cell in self.shape.points.iter() { | |
let p = cell + self.position; | |
println!("{:?} = {:u}", p, self.grid.data[p.x][p.y]); | |
s += self.grid.data[p.y][p.x]; | |
} | |
s | |
} | |
} | |
pub struct Point { | |
x: int, | |
y: int, | |
} | |
impl Add<Point, Point> for Point { | |
fn add(&self, rhs: &Point) -> Point { | |
Point {x: self.x + rhs.x, y: self.y + rhs.y} | |
} | |
} | |
pub struct Shape { | |
points: [Point, ..4], | |
} |
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 grid::Grid; | |
use pieces::{Piece,Point,Shape}; | |
mod grid; | |
mod pieces; | |
fn main() { | |
let mut g = Grid::random(6, 6); | |
let shape_t = Shape {points: [Point {x: 1, y: 0}, Point {x: 0, y: 1}, Point {x: 1, y: 1}, Point {x: 2, y: 1}]}; | |
let p = Piece {grid: &g, shape: &shape_t, position: Point {x: 0, y: 0}}; | |
g.highlight = Some(~p); | |
print!("{}", g); | |
println(""); | |
println(fmt!("%?", p.sum())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment