Module 在生锈的模块内部使用模块
我在Module 在生锈的模块内部使用模块,module,rust,Module,Rust,我在src目录中有一个文件spolder.rs。它目前由main.rs使用。但是,我要求共享同一目录的文件FreeFall.rs在spolution.rs中使用。以下是目前的情况: 目录 src |___梅因 |___1.3卢比 |___自由落体 MAIN.RS mod Projectile; fn main() { println!("Goed... momenteel"); Projectile::projectile(10.0, 5.0, 100.0, 7.5); }
src
目录中有一个文件spolder.rs。它目前由main.rs使用。但是,我要求共享同一目录的文件FreeFall.rs在spolution.rs中使用。以下是目前的情况:
目录
src
|___梅因
|___1.3卢比
|___自由落体
MAIN.RS
mod Projectile;
fn main() {
println!("Goed... momenteel");
Projectile::projectile(10.0, 5.0, 100.0, 7.5);
}
use std;
mod FreeFall;
pub fn projectile(init: f64, AngleX: f64, AngleY: f64, AngleZ: f64) {
let mut FFAccel = FreeFall::acceleration();
struct Velocities {
x: f64,
y: f64,
z: f64,
t: f64,
};
let mut Object1 = Velocities {
x: init * AngleX.sin(),
y: init * AngleY.cos(),
z: init * AngleZ.tan(),
t: (2.0 * init.powf(-1.0) * AngleY.sin()) / FFAccel,
};
println!("{}", Object1.t);
}
use std;
pub fn acceleration() {
// maths here
}
投射物.RS
mod Projectile;
fn main() {
println!("Goed... momenteel");
Projectile::projectile(10.0, 5.0, 100.0, 7.5);
}
use std;
mod FreeFall;
pub fn projectile(init: f64, AngleX: f64, AngleY: f64, AngleZ: f64) {
let mut FFAccel = FreeFall::acceleration();
struct Velocities {
x: f64,
y: f64,
z: f64,
t: f64,
};
let mut Object1 = Velocities {
x: init * AngleX.sin(),
y: init * AngleY.cos(),
z: init * AngleZ.tan(),
t: (2.0 * init.powf(-1.0) * AngleY.sin()) / FFAccel,
};
println!("{}", Object1.t);
}
use std;
pub fn acceleration() {
// maths here
}
自由落体.RS
mod Projectile;
fn main() {
println!("Goed... momenteel");
Projectile::projectile(10.0, 5.0, 100.0, 7.5);
}
use std;
mod FreeFall;
pub fn projectile(init: f64, AngleX: f64, AngleY: f64, AngleZ: f64) {
let mut FFAccel = FreeFall::acceleration();
struct Velocities {
x: f64,
y: f64,
z: f64,
t: f64,
};
let mut Object1 = Velocities {
x: init * AngleX.sin(),
y: init * AngleY.cos(),
z: init * AngleZ.tan(),
t: (2.0 * init.powf(-1.0) * AngleY.sin()) / FFAccel,
};
println!("{}", Object1.t);
}
use std;
pub fn acceleration() {
// maths here
}
我不能只使用9.81(地球上的平均重力),因为它没有考虑空气阻力、终端速度等
我尝试将
FreeFall
模块包含到main.rs中,但没有成功。在Rust中,单文件模块无法使用mod
关键字从其他文件声明其他模块。为此,您需要为模块创建一个目录,将模块根文件命名为mod.rs
,并在其中为每个嵌套模块创建一个单独的文件(或目录)
根据经验,您应该在目录的“根文件”(通常是main.rs
、lib.rs
或mod.rs
)中声明每个模块,并在其他地方使用它。方便的是,您可以使用cratet::
作为您的cratet的根模块,并使用它引用您的模块
例如:
src/
主集装箱
foo.rs板条箱::foo
bar.rs板条箱::bar
巴兹/
mod.rs板条箱::baz
内包装箱::baz::内包装箱
main.rs
// declare the modules---we only do this once in our entire crate
pub mod foo;
pub mod bar;
pub mod baz;
fn main() { }
// use other modules in this crate
use crate::bar::*;
use crate::baz::inner::*;
// the baz module contains a nested module,
// which has to be declared here
pub mod inner;
foo.rs
// declare the modules---we only do this once in our entire crate
pub mod foo;
pub mod bar;
pub mod baz;
fn main() { }
// use other modules in this crate
use crate::bar::*;
use crate::baz::inner::*;
// the baz module contains a nested module,
// which has to be declared here
pub mod inner;
baz/mod.rs
// declare the modules---we only do this once in our entire crate
pub mod foo;
pub mod bar;
pub mod baz;
fn main() { }
// use other modules in this crate
use crate::bar::*;
use crate::baz::inner::*;
// the baz module contains a nested module,
// which has to be declared here
pub mod inner;
进一步阅读的一些资源:
freefall.rs
移动到名为sparlode
的子目录中。还要注意,使用std代码>不起任何作用,而且模块名称按惯例是小写的。谢谢,非常感谢您应该努力避免将文件与模块混淆。文件可能包含多个模块(但模块可能不会像某些其他语言那样分布在多个文件中)。例如,您可以将freefall.rs
的内容直接放在mod freefall{/*here*/}
中,而不必创建新文件<代码>大写
用于类型和枚举变量;对于静力学和常数,SNAKE\u案例
。使用射弹
<请改为代码>自由落体。谢谢。我没有意识到我必须那样做。