Rust无法将Singleton从全局空间导入另一个文件中的另一个模块 [英] Rust Can't import Singleton From global space into another module in another file

查看:117
本文介绍了Rust无法将Singleton从全局空间导入另一个文件中的另一个模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用lazy_static库创建一个单例.我无法访问另一个文件中模块中的那个单例.如果模块是在主要功能下定义的,那么我可以正常访问它.

Using the lazy_static library to create a singleton. I am unable to access that singleton in a module in another file. If the module is defined below the main function I can access it fine.

给出如下主文件,test_stuff可以很好地访问游戏数据:

Given a main file such as the following, test_stuff can access gamedata fine:

extern crate gamedata;
#[macro_use]
extern crate lazy_static;


lazy_static! {
    pub static ref GAMEDATA: &'static str = "I am a test STrings";
} 

fn main() {
    println!("Game Data:{}",*GAMEDATA);
    let obj = gamedata::readinginstatic::readinginstatic {
        structure:"working".to_string()
    };
    obj.print_static();
}

mod test_stuff {
    use ::GAMEDATA;
    fn printthing() {
        println!("herenow:{}", *GAMEDATA);
    }
}

带有lib.rs文件,例如:

pub mod readinginstatic;

和另一个文件readinginstatic.rs中的模块,如下所述:

And a module in another file readinginstatic.rs as described below:

use ::GAMEDATA;

pub struct readinginstatic {
    pub structure:String,
}

impl readinginstatic {
    pub fn print_static(&self) {
        println!("In Print static width:{}", *::GAMEDATA);
    }
}

我得到了错误:

在板条箱根中找不到

not found in the crate root

尝试导入GAMEDATA时.

如果在另一个文件中定义了lazy_static单例,是否可以访问另一个模块中的单例?

Is it possible to access a lazy_static singleton in another module if it is defined in another file?

为确保提供了最小,完整和可验证的示例,这是我在GitHub上的整个示例代码: https ://github.com/camccar/moduleError

To insure I have provided a Minimal, Complete, and Verifiable example here is my whole example code on GitHub: https://github.com/camccar/moduleError

推荐答案

::GAMEDATA引用gamedata板条箱的板条根中的某个名为GAMEDATA的值.但是,定义GAMEDATA的位置不在该板条箱中,而是在具有gamedata作为依赖项的main.rs文件中.

::GAMEDATA refers to some value called GAMEDATA in the crate root of the gamedata crate. However where you defined GAMEDATA was not in that crate, it was in your main.rs file which has gamedata as a dependency.

因此,您要在此处进行的操作是从板条箱中伸出援手,使用取决于您的板条箱中的某些物品,我不确定,但我认为这是不允许的.

So what you're trying to do here is reach out of a crate use something from the crate that is depending on you, which I'm not sure but I don't think is allowed.

您可以考虑将其反转,而不是在gamedata板条箱中初始化GAMEDATA,如果您需要在main中使用它,则可以正常地use进行

You could consider inverting this and instead initialising GAMEDATA inside your gamedata crate instead and if you need to use it in main you can just use it normally:

extern crate gamedata;

use gamedata::GAMEDATA;

fn main(){
    println!("Game Data:{}", *GAMEDATA);

    ...
}

或者,如果GAMEDATA不是您的游戏数据箱应该知道的定义,则可以在main内部构造它,并将其作为参数传递给gamedata箱中的某个函数.

Alternatively if GAMEDATA is not something your game data crate should know how to define, you could construct it inside main and pass it to some function in the gamedata crate as a parameter.

这篇关于Rust无法将Singleton从全局空间导入另一个文件中的另一个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆