why doesnt this work?
i made a library with "cargo new name --lib"
i added chrono in the toml file as a dependency
i added "use chrono::Utc;" at the top of the file
this example library only has one macro in it that prints out the current date using chrono in a certain format
for some reason the rust compiler says chrono is an unused import which is clearly false
also when im trying to use my library from a binary project the library and the macro inside it can be seen but i get an error of "use of undeclared type
Ape Out Shirt $21.68 |
Rust compiler can't evaluate imports from macros since they are compile time. If you don't use the macro, the import is never used either
ah i see, and how can i make this work? because for what i need it has to be a macro
Import chrono at calling site
I don't use rust but probably just suppress the warning somehow.
No, if macro is not used, the statement in macro is never used either. The compiler can tell if a macro is ever called, but can't tell for functions because they can be called unconventionally. That's why everything inside a function is considered used
>Rust compiler can't evaluate imports
>since they are compile time
So what you're saying is that you rust doesn't support static linkage?
Macros has nothing to do with linkage, Anon.
thanks
dumb morons
Ignore all these other morons
Just move the import inside the macro
thanks anon you are amazing, its fixed in the library. but now when im using the library in a binary crate it says use of undeclared crate or module chrono. am i still missing something?
One option might be to just expand the macro out to the full name without doing any use statements.
Oh, shit
I actually tried it out before I posted, and it worked fine, but I guess your setup is different
Maybe it's better to use #[allow(unused_imports)] or whatever
Yeah sure thanks for the advice
The quickest fix is to make the binary crate depend on chrono. The macro more or less just copy/pastes code into the binary crate, so it doesn't get access to chrono automatically.
A more sophisticated and arguably better solution is to do this:
pub use chrono;
#[macro_export]
macro_rules! something {
... $crate::chrono::Utc::now() ...
}
This exposes chrono through the library crate. Let's say your library crate is called your_library, then thanks to the "pub use" your binary crate is able to do your_library::chrono::Utc::now(). And $crate is a magic variable that expands to your_library.
You're doing everything correctly
It would seem you forgot to dilate your neovegana today, do that in front of your webcam and Mr. Shekelberg will unbrick your code
troony thread.
If this was a genuine question it would be posted in /sqt/ or /dpt/ but it isn't.
Trannies need the drama and attention.
Do not reply to the troony thread.
nobody forced your hand to open this thread moron
>rust gods
it's goddesses
*xoddess, chud
By His grace, this entire thread must be purged.
Medication.
If you add pub use something; does it help? That's the newer syntax I think.
no it didnt
just change Utc::now() to chrono::Utc::now()
What you need is to use the salt library, and wait until rust settles in.
What should I build in rust? I wanna learn shit
Imageboard engine.
>Imageboard engine.
A good one. This could be actually fun.
Not that fun tbh
A doubly linked list
try to port https://winter.dev/articles/falling-sand-worlds to rust you will learn to hate/love rust depending on how good you are at software design.
Who designed this Syntax and thought it was good?
>muh syntax
dumb moronic LARPer