4

I want to call a function inside a main.rs file. I have made one directory name "library" inside the same src folder as main.rs exist.

src/main.rs

mod library;

fn main() {
   println!("{}", library::name1::name(4));
}

src/library/file.rs

pub mod name1 {
    pub fn name(a: i32) -> i32 {
        println!("from diff file {}", a);
        a * a
    }
}

when I call this function name in main.rs, compiler throws an error:

error[E0583]: file not found for module library

I think I am missing something. What is the correct way to do this? Keep in mind that the library directory is just an ordinary directory not a cargo package

Jawwad Turabi
  • 322
  • 4
  • 12
  • No its not from another file it is to call the function from another directory in the same src directory – Jawwad Turabi Nov 14 '19 at 17:56
  • Hi there! Your problem is not about calling the function. The error message you posted is about your `mod library;` statement. The Rust compiler should show that line below the error message. In short, the solution to your problem is: rename`library/file.rs` to `library/mod.rs`. You should also remove `pub mod name1` as one usually does not use inline `mod` definitions. I hope this helps. But I recommend you to read through the answers in the linked questions. – Lukas Kalbertodt Nov 14 '19 at 23:05

3 Answers3

2

You can fix this issue in 2 different ways:

1 ) Using Module 2 ) Using Library

Using Module

Simply create a file next to main.rs in src directory and name it name1.rs

name1.rs will look like:

//no need to specify "pub mod name1;" explicitly
pub fn name(a: i32) -> i32 {
   println!("from diff file {}", a);
   a * a
}

main.rs will look like:

//name of the second file will be treated as module here
pub mod name1;

fn main() {
   println!("{}", name1::name(4));
}

Using Library

a) create a library, standing in main project directory (i.e. parent of src directory) and run the command below:

//In your case library name "file"     
$ cargo new --lib file

This command will create another directory of name file same as your main project.

b) Add this library(file) in the dependency section of Cargo.toml file of main project

[package]
name = "test_project"
version = "0.1.0"
authors = ["mohammadrajabraza <mohammadrajabraza@gmail.com>"]
edition = "2018"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
file = {path ="file"}

c) A file under main_project>file(library)>src>lib.rs will be created, once you created library using command above.

lib.rs will be look like:

pub fn name(a: i32) -> i32 {
             println!("from diff file {}", a);
             a * a
}

d) and finally your main.rs will be:

//importing library in thescope
use file;

fn main() {
   println!("{}", file::name(4));
}
rajabraza
  • 333
  • 3
  • 11
1

Create a file in src directory named as library.rs and than in library.rs write the following code:

pub mod file;

Than access the same function from your main file as you are doing right now.Than it will work properly.

You can follow this method too

https://github.com/Abdul-sid/PIAIC-IOT-RUST-CLASS/tree/master/13th-Oct-chapter-7-part-2/dir-mod-bin/src

0

You can use the concept of library which rust have in it. Simply making a library project having the --lib flag.

cargo build library --lib

After doing it. You will write this in your dependency section in Cargo.toml.

library = {path = "./src/library"}  

You can even use the absolute path of your directory.

library = {path = "C:/Users/Admin/Desktop/Rust/jawwad/src/library"}  

Then write your library code in lib.rs file.

pub mod name1 {
    pub fn name(a: i32) -> i32 {
          println!("from diff file {}", a);
          a * a
    }
}

Here is the main.rs file.

use library;

fn main() {
   println!("{}", library::name1::name(4));
}
Fahim Uz Zaman
  • 444
  • 3
  • 6