- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 rust 比较陌生,所以以下也可能是对一个概念的误解:
我从 basic project template 中获取了 ggez 基本项目模板。看起来像这样:
use ggez::{graphics, Context, ContextBuilder, GameResult};
use ggez::event::{self, EventHandler};
fn main() {
// Make a Context.
let (mut ctx, mut event_loop) = ContextBuilder::new("my_game", "Cool Game Author")
.build()
.expect("aieee, could not create ggez context!");
// Create an instance of your event handler.
// Usually, you should provide it with the Context object to
// use when setting your game up.
let mut my_game = MyGame::new(&mut ctx);
// Run!
match event::run(&mut ctx, &mut event_loop, &mut my_game) {
Ok(_) => println!("Exited cleanly."),
Err(e) => println!("Error occured: {}", e)
}
}
struct MyGame {
// Your state here...
}
impl MyGame {
pub fn new(_ctx: &mut Context) -> MyGame {
// Load/create resources such as images here.
MyGame {
// ...
}
}
}
impl EventHandler for MyGame {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
// Update code here...
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
graphics::clear(ctx, graphics::WHITE);
// Draw code here...
graphics::present(ctx)
}
}
并将其重构为两个文件
main.rs
和
game.rs
第一个刚刚包含
main()
函数,其他的都进入
game.rs
.
main.rs
中更改导入后到
mod game;
use game::MyGame;
use ggez::{graphics, Context, ContextBuilder, GameResult};
use ggez::event::{self, EventHandler};
并将其添加到
game.rs
use ggez::event::EventHandler;
use ggez::{Context, GameResult, graphics};
一切正常,只要我做
MyGame
上市。
pub (in infinite_runner::main)
和类似的,例如
crate::main
但没有接受各种错误消息。
MyGame
至
main.rs
不暴露给其他人?似乎 main 不是一个有效的目标。
最佳答案
通过重申 rust 文档,我设法弄清楚了。
虽然回答我自己的问题感觉有点可疑,但实际上可能对其他人有帮助:
TLDR;
我们希望 main 函数内的代码能够访问 MyGame,但外部的任何其他模块都拒绝访问。
是否可以将代码分解为 game.rs
: 不。
是否可以做到:是的。
以下是原因和方法:
来自 rust 文档 Visibility and Privacy :
With the notion of an item being either public or private, Rust allowsitem accesses in two cases:
- If an item is public, then it can be accessed externally from somemodule m if you can access all the item's parent modules from m. Youcan also potentially be able to name the item through re-exports. Seebelow.
- If an item is private, it may be accessed by the current moduleand its descendants.
main.rs
位于顶层的任何相同级别的公共(public)内容都可以像任何其他模块一样被整个 crate 访问,因为每个模块都可以访问顶层的父级。
lib.rs
的文件中那么唯一的区别就是路径,如
lib.rs
顶层隐含的只是
crate
路径,而在名为
game.rs
的文件中路径将是
crate::game
.
game
的目录并移动
game.rs
在此目录中并将 pub 关键字添加到 MyGame:
pub struct MyGame
.
__init__.py
文件 rust 需要一个文件
mod.rs
使目录成为模块。
mod.rs
你声明你里面的文件,
mod game
在我们的例子中。
crate::game::game::MyGame
来寻址 MyGame。 , 但是自从
game.rs
被声明为私有(private),对 MyGame 的访问是密封的,因为路径的所有元素都必须是公共(public)的。
main.rs
进入游戏目录,但我们可以将其中的代码分解为另一个函数。我们就叫它
init
因为缺乏幻想。我们将 init 函数放在一个名为
init.rs
的文件中。在游戏目录中并在
mod.rs
中将其声明为 public像这样:
pub mod init
.
game::init::init()
因为它是公开的,但不是
game::game::MyGame
.然而,Init 可以访问
MyGame
.
src
|---main.rs
|---game
|---mod.rs
|---game.rs
|---init.rs
main.rs:
mod game;
use game::init;
fn main() {
init::init();
}
模组.rs:
pub mod init;
mod game;
游戏.rs:
use ggez::event::EventHandler;
use ggez::{graphics, Context, GameResult};
pub struct MyGame {
// Your state here...
}
impl MyGame {
pub fn new(_ctx: &mut Context) -> MyGame {
// Load/create resources such as images here.
MyGame {
// ...
}
}
}
impl EventHandler for MyGame {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
// Update code here...
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
graphics::clear(ctx, graphics::WHITE);
// Draw code here...
graphics::present(ctx)
}
}
初始化.rs:
use crate::game::game::MyGame;
use ggez::{ContextBuilder, event};
pub fn init() {
// Make a Context.
let (mut ctx, mut event_loop) = ContextBuilder::new("my_game", "Cool Game Author")
.build()
.expect("aieee, could not create ggez context!");
// Create an instance of your event handler.
// Usually, you should provide it with the Context object to
// use when setting your game up.
let mut my_game = MyGame::new(&mut ctx);
// Run!
match event::run(&mut ctx, &mut event_loop, &mut my_game) {
Ok(_) => println!("Exited cleanly."),
Err(e) => println!("Error occured: {}", e),
}
}
关于Rust:重构后结构的可见性现在公开;无法将 main 添加到 pub(在 crate::main 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62603528/
我目前正在尝试基于哈希表构建字典。逻辑是:有一个名为 HashTable 的结构,其中包含以下内容: HashFunc HashFunc; PrintFunc PrintEntry; CompareF
如果我有一个指向结构/对象的指针,并且该结构/对象包含另外两个指向其他对象的指针,并且我想删除“包含这两个指针的对象而不破坏它所持有的指针”——我该怎么做这样做吗? 指向对象 A 的指针(包含指向对象
像这样的代码 package main import "fmt" type Hello struct { ID int Raw string } type World []*Hell
我有一个采用以下格式的 CSV: Module, Topic, Sub-topic 它需要能够导入到具有以下格式的 MySQL 数据库中: CREATE TABLE `modules` ( `id
通常我使用类似的东西 copy((uint8_t*)&POD, (uint8_t*)(&POD + 1 ), back_inserter(rawData)); copy((uint8_t*)&PODV
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我有一个指向结构的指针数组,我正在尝试使用它们进行 while 循环。我对如何准确初始化它并不完全有信心,但我一直这样做: Entry *newEntry = malloc(sizeof(Entry)
我正在学习 C,我的问题可能很愚蠢,但我很困惑。在这样的函数中: int afunction(somevariables) { if (someconditions)
我现在正在做一项编程作业,我并没有真正完全掌握链接,因为我们还没有涉及它。但是我觉得我需要它来做我想做的事情,因为数组还不够 我创建了一个结构,如下 struct node { float coef;
给定以下代码片段: #include #include #define MAX_SIZE 15 typedef struct{ int touchdowns; int intercepti
struct contact list[3]; int checknullarray() { for(int x=0;x<10;x++) { if(strlen(con
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Empty “for” loop in Facebook ajax what does AJAX call
我刚刚在反射器中浏览了一个文件,并在结构构造函数中看到了这个: this = new Binder.SyntaxNodeOrToken(); 我以前从未见过该术语。有人能解释一下这个赋值在 C# 中的
我经常使用字符串常量,例如: DICT_KEY1 = 'DICT_KEY1' DICT_KEY2 = 'DICT_KEY2' ... 很多时候我不介意实际的文字是什么,只要它们是独一无二的并且对人类读
我是 C 的新手,我不明白为什么下面的代码不起作用: typedef struct{ uint8_t a; uint8_t* b; } test_struct; test_struct
您能否制作一个行为类似于内置类之一的结构,您可以在其中直接分配值而无需调用属性? 前任: RoundedDouble count; count = 5; 而不是使用 RoundedDouble cou
这是我的代码: #include typedef struct { const char *description; float value; int age; } swag
在创建嵌套列表时,我认为 R 具有对列表元素有用的命名结构。我有一个列表列表,并希望应用包含在任何列表中的每个向量的函数。 lapply这样做但随后剥离了列表的命名结构。我该怎么办 lapply嵌套列
我正在做一个用于学习目的的个人组织者,我从来没有使用过 XML,所以我不确定我的解决方案是否是最好的。这是我附带的 XML 文件的基本结构:
我是新来的 nosql概念,所以当我开始学习时 PouchDB ,我找到了这个转换表。我的困惑是,如何PouchDB如果可以说我有多个表,是否意味着我需要创建多个数据库?因为根据我在 pouchdb
我是一名优秀的程序员,十分优秀!