gpt4 book ai didi

module - 将模块作为参数传递的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-29 08:10:48 25 4
gpt4 key购买 nike

我有一个模块文件(src/map/map.rs):

type Layer = Vec<Vec<Tile>>;

pub struct Map {
height: i32,
width: i32,
data: Layer,
rooms: Vec<Rect>,
}

impl Map {
pub fn new(width: i32, height: i32) -> Self {
Map {
height: height,
width: width,
data: vec![vec![Tile::wall(); height as usize]; width as usize],
rooms: vec![Rect],
}
}
pub fn generate_with(&self, creator: module) {
creator::generate(&self)
}
}

嵌套模块 map::gen::dungeon::basic (src/map/gen/dungeon/basic.rs)文件中有一个函数:

pub fn generate(map: &mut Map) -> (Map, (i32, i32)) {}

map模块文件(src/map/mod.rs):

mod rect;
mod tile;
mod map;
pub mod room;
pub mod gen;

pub use self::map::Map;
pub use self::rect::Rect;
pub use self::tile::Tile;

像这样导入到 main.rs 中:

mod map;

use map::*;
use map::gen;

我希望能够像这样使用它:

let (map, (player_x, player_y)) = Map::new(MAP_WIDTH, MAP_HEIGHT).generate_with(gen::dungeon::basic);

我得到的错误是:

[cargo] expected value, found module 'gen::dungeon::basic': not a value [E]

一个完整的repo is available .

最佳答案

如评论中所述,模块不是这样的具体概念;你试图做的事是不可能的。

相反,您可以传递可以作为值的内容:

mod basic {
pub fn generate() -> u8 {
0
}
}

mod advanced {
pub fn generate() -> u8 {
42
}
}

fn play_the_game(generator: fn() -> u8) {
let dungeon = generator();
println!("{}", dungeon);
}

fn main() {
play_the_game(basic::generate);
play_the_game(advanced::generate);
}

您还可以引入特征并将实现类型作为泛型传递:

trait DungeonGenerator {
fn generate() -> u8;
}

mod basic {
use DungeonGenerator;

pub struct Basic;

impl DungeonGenerator for Basic {
fn generate() -> u8 {
0
}
}
}

mod advanced {
use DungeonGenerator;

pub struct Advanced;

impl DungeonGenerator for Advanced {
fn generate() -> u8 {
42
}
}
}

fn play_the_game<G>()
where
G: DungeonGenerator,
{
let dungeon = G::generate();
println!("{}", dungeon);
}

fn main() {
play_the_game::<basic::Basic>();
play_the_game::<advanced::Advanced>();
}

关于module - 将模块作为参数传递的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49286608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com