I know the title sounds weird, but basically I am creating a mathematics library in Rust. I have 1 Struct that has Basic functions like Differentiation, Newton's Method, Series, etc. but I also have numerical Integration Algorithms. The issue I have is that I want to call my Integration Algorithms through crate_name::Functions::Integration::<name_of_function>
but I haven't figured out a way. I really don't know where to start when it comes to find a solution to this and that why I came here.
This is my src/
directory for reference:
src/
...lib.rs
...func/
......mod.rs
......functions.rs
......integration.rs
if you need it here is the github for the directory in question: https://github.com/VLambda/NumeriLib/tree/main/src/func
mod.rs:
mod functions;
mod integration;
pub use functions::*;
functions.rs:
use crate::func::integration::Integration;
pub struct Functions;
impl Functions {
pub fn right_riemann<F: Fn(f64) -> f64>(
function: F,
lower_limit: f64,
upper_limit: f64,
intervals: f64,
) -> f64 {
Integration::right_riemann(function, lower_limit, upper_limit, intervals)
}
}
integration.rs:
pub struct Integration;
impl Integration {
...
}
So far I've made it so that you would need to call the functions for Integration via crate_name::Functions::<name_of_function>
but I think it would be better if I can call it through crate_name::Functions::Integration::<name_of_function>
. Is this doable with my current code, or do I need to drastically change the project structure to accommodate this?
Rust isn't Java. In Rust you don't need to create an empty struct
just to hold free functions, you can (and should) simply put the free functions directly in a module:
pub mod functions {
pub mod integration {
pub fn riemann() { todo!(); }
}
}
Then you can call the function with just functions::integration::riemann()
And of course you can split the modules into individual files if you want to:
/// src/lib.rs
pub mod functions;
// src/functions.rs
pub mod integration;
// src/functions/integration.rs
pub fn riemann() { todo!(); }