Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(unused_variables, dead_code)]
- use std::fmt::Display;
- const EMPTY_CHAR: &str = " ";
- struct Point {
- x: usize,
- y: usize,
- }
- struct Character {
- position: Point,
- symbol: String,
- }
- struct Game<'a> {
- arena: Vec<Vec<&'a str>>,
- characters: Vec<Character>,
- }
- impl Game<'_> {
- fn new(size: (usize, usize)) -> Game<'static> {
- Game {
- arena: vec![vec![EMPTY_CHAR; size.0]; size.1],
- characters: vec![],
- }
- }
- fn add_character(&mut self, character: Character) {
- let position = &character.position;
- // ERROR HERE character.symbol does not live enough
- self.arena[position.x][position.y] = &character.symbol;
- // ERROR HERE cannot move out of character because it is borrowed
- // move out of character occurs here
- self.characters.push(character);
- }
- }
- impl Display for Game<'static> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- for line in &self.arena {
- for cell in line {
- print!("{}", cell);
- }
- println!();
- }
- std::fmt::Result::Ok(())
- }
- }
- fn main() {
- let mut game = Game::new((20, 10));
- let prince = Character {
- position: Point { x: 3, y: 4 },
- symbol: String::from("P"),
- };
- game.add_character(prince);
- println!("{}", game);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement