How to share a module between several projects?

I created module time. Now, I'm going to use it in my projects. There is no problem if I refer it from projects code as:

use std::time::{SystemTime};

use crate::time::DAYS_OF_WEEK; ---> need to comment out to prevent 'unresolved import'

fn main() {
    println!{"{}", time()}
}

fn time() -> String {
    let dur = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
    let (y,m,d,h,min,s,w) = time::get_datetime(1970, dur.as_secs());

    format!("{m:0>2}-{:0>2}-{:0>2} {}, {:0>2}:{:0>2}:{:0>2} GMT", d,y, time::DAYS_OF_WEEK[w as usize],h,min,s)
}

Why refering the module directly from code works, but directive use?

Your sentence fell apart at the end there; I don't think that's what you meant to say. I have no idea what you're asking. Could you reword your question?

Why can't I use "time::DAYS_OF_WEEK" without "time::" prefix in the code ?

Заслано с ΠΌΠΎΠ΅Π³ΠΎ LG G8, ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΡ€ΠΈΠ²ΠΎ をむドル

Thank you, John . You helped me a lot. I added:

extern crate time;

and it solved the problem. You can close the thread .

Заслано с ΠΌΠΎΠ΅Π³ΠΎ LG G8, ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΡ€ΠΈΠ²ΠΎ をむドル

If you have both lib.rs and main.rs, then main.rs can only use the public API for the library, and typically doesn't have any modules of its own (be careful not to have the same mod twice, because that's super confusing).

If you only have main.rs, then you're missing mod time to actually create it. Files on disk don't do anything by themselves, and modules don't exist until you create them with mod.

You can't share just a module between crates (projects). You can only share whole library crates between projects.

3 Likes

A question in the consistency of the implementation. The linking of a library on the level of calls or specifying a definition of the possibility to be linked is implemented by different people. I will write a blog regarding the consistency of Rust implementation, because it's a serious problem.

Заслано с ΠΌΠΎΠ΅Π³ΠΎ LG G8, ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΡ€ΠΈΠ²ΠΎ をむドル

МнС каТСтся, Π²Ρ‹ ΠΏΠ»ΠΎΡ…ΠΎ ΠΏΠΎΠ½ΠΈΠΌΠ°Π΅Ρ‚Π΅ Ρ€Π°Π·Π½ΠΈΡ†Ρƒ ΠΌΠ΅ΠΆΠ΄Ρƒ модулями, ΠΊΡ€Π΅ΠΉΡ‚Π°ΠΌΠΈ, опрСдСлСниями ΠΌΠΎΠ΄ΡƒΠ»Π΅ΠΉ (mod foo;) ΠΈ модификациями нСймспСйсинга (use path;).

You are absolutely right, it just tells about the bad design of Rust there. Too many useless moving parts. Nobody can explain to me why I can call an external method directly, however creating a short cut to it is a much pain.

Заслано с ΠΌΠΎΠ΅Π³ΠΎ LG G8, ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΡ€ΠΈΠ²ΠΎ をむドル

I'm having trouble understanding. Are you using FFI? Were you able to call a function through FFI? What do you mean by "creating a short cut to it"?

1 Like

You can use a fully qualified name of the function, or just a short name hiding all details in Β«useΒ» directive.

Заслано с ΠΌΠΎΠ΅Π³ΠΎ LG G8, ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΡ€ΠΈΠ²ΠΎ をむドル

Thanks, I understand now. That is true of Rust, and also of many other languages (e.g., Java). Some of the reasons this is necessary are:

  • Without the use feature, programs would be very verbose since the fully qualified name of every type and free function (that is not defined in the current module) would have to be specified. Therefore, it is normally best to use the module path, or perhaps the partial path, of these types and functions, to make the program easier to read.
  • But we cannot always use the unqualified name of a function or type, because within one module we may need to call two different functions or use two different types that have the same name but are in two different modules. Therefore we must sometime qualify the function or type name with a full module path or partial module path.

Yes, editing the use statements can be painful. Be sure to use an IDE or editor that supports rust-analyzer (e.g., vscode, emacs, nvim). The IDE/editor will make it very easy to add and maintain the use statement. If you type a function or type name that is not currently in scope, it will allow you to select from one or more modules where that name appears. When you select one, it will add the use statement for you automatically.

You will also see warnings when you added a use statement that is no longer needed. There will be a "quick fix" option to delete it. There are also options to automatically organize the use statements alphabetically and consolidate them when possible.

These two sections of "the book" may help:
Paths for Referring to an Item in the Module Tree
Bringing Paths into Scope with the use Keyword

If I understand your question, you asked how to share a module with other modules in one package, and how refer to two modules with the same base name (time).

If that is correct, could you please change the title of the thread to say "modules" instead of "projects"? I think this is part of why there is some confusion.

I think it's pretty clear that the OP's time is not std::time, as that does not have a get_datetime() function.

Yes, thanks. I'll correct that in my post.


I ended up deleting most of it.

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.