我有一个简单的代码,它导入两个模块并从它们中使用struct。
在main.rs中,我使用来自bots/maintrait.rs和gamecore\board.rs的函数--它们都是以相同的方式导入的,但是maintrait.rs的func无法解析。
下面是我的src目录的结构:
.
├── bots
│ ├── maintrait.rs
│ └── mod.rs
├── gamecore
│ ├── board.rs
│ └── mod.rs
└── main.rs和代码:
main.rs
use gamecore::{GameBoard,State};
use bots::{Bot,DummyBot};
mod bots;
mod gamecore;
fn main() {
let board = GameBoard::new();
let bot = DummyBot::new(State::O);
board.make_turn(State::X, (0, 0));
board.make_turn(State::O, bot.get_move(&board));
}gamecore\mod.rs
pub use self::board::{GameBoard,State};
mod board;gamecore\board.rs
pub struct GameBoard {
field: [[State, ..3], ..3]
}
impl GameBoard {
pub fn new() -> GameBoard {
GameBoard {
field: [[State::Empty, ..3], ..3]
}
}
...
}bots\mod.rs
pub use self::maintrait::{Bot,DummyBot};
mod maintrait;bots\maintrait.rs
use gamecore::{GameBoard,State};
use std::rand;
pub trait Bot {
fn new<'a>() -> Box<Bot + 'a>;
fn get_move(&mut self, board: &GameBoard) -> (uint, uint);
}
pub struct DummyBot {
side: State
}
impl Bot for DummyBot {
fn new<'a>(side: State) -> Box<Bot + 'a> {
box DummyBot{
side: side
}
}
fn get_move(&mut self, board: &GameBoard) -> (uint, uint) {
let turn = rand::random::<uint>() % 9;
(turn / 3, turn % 3)
}
}错误信息
10:28 error: failed to resolve. Use of undeclared module `DummyBot`
let bot = DummyBot::new(State::O);
^~~~~~~~~~~~~
10:28 error: unresolved name `DummyBot::new`
let bot = DummyBot::new(State::O);
^~~~~~~~~~~~~我哪里错了?为什么两个相同的进口不同?
发布于 2014-12-13 15:51:33
生锈有一个很好的例子来说明如何做类似的事情。
以下是需要更改的适当代码片段:
pub trait Bot {
// Your trait and implementation signatures differ, so I picked this one
fn new() -> Self;
}
impl Bot for DummyBot {
fn new() -> DummyBot {
DummyBot{
side: State::Empty
}
}
}
let bot: DummyBot = Bot::new();我猜有一点,但我认为根本原因是您没有真正定义DummyBot::new,而是定义了一个DummyBot碰巧实现的通用Bot::new。您必须调用已定义的方法(Bot::new),并提供足够的信息来消除调用(let的类型)的歧义。
https://stackoverflow.com/questions/27460453
复制相似问题