i am very new to rust programming and i am writing a code in which i have 3 files under src folder i.e main.rs, mod_a.rs and mod_b.rs and i want to access mob_b.rs in mod_a.rs i have already written mod mod_b in main.rs and use crate::mod_b::function2; (function2 is defined in my mod_b.rs) in mod_a.rs but i wont compile it will giver error. i am attaching the pictures of code below please have alook in it i am very confuse. thanks in advance for your time and consideration.
main.rs
mod_a.rs:
mod_b.rs:
function1
doesn't return anything, so you cannot assign the result of it back into input_data
. It would work if function1
looked like this:
pub fn function1(data1: f32) -> f32 {
function2(data1 * 9.0)
}
The differences are:
- A return type has been added to the function signature (
-> f32
). - The semi-colon (
;
) has been removed from the function body (using a semi-colon throws the value away and returns nothing.
(Also, in future, please provide your code/error messages as text rather than as images - it makes it easier for people to copy and paste your code so they can help fix it!)
thanks for your kind response brother can u plz help i want to store value from function2 in mod_b.rs in input_data how to write the code for this .
Are you asking how to call function2
from main
? If so:
mod mod_a;
mod mod_b;
fn main() {
let mut input_data: f32 = 46.5;
input_data = mod_b::function2(input_data);
println!("{}", input_data);
}
If you still want to call function2
via function1
, make the change I showed you in my previous reply instead.
And when you do this, please follow the formatting guide for your code
https://users.rust-lang.org/t/forum-code-formatting-and-syntax-highlighting/42214/7
thanks for your reply but i want to call function2 from function1 and the vaue return from function2 i want to store that value in input_data variable in main.
Then you need to return the output of function2
from function1
, as shown in my first reply. You can't return the value directly from function2
to main
without passing it up the stack via function1
.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.