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?
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.
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, ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΡΠΈΠ²ΠΎ γ’γ€γγ«
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, ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΡΠΈΠ²ΠΎ γ’γ€γγ«
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.
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.