How to import everything from the crate root? - rust

To import symbols from a module you either need to enumerate them or use a wildcard to import everything. That is, I can use either use module::{SomeSymbol, SomeOtherSymbol}; or use module::*;
However, when importing from the top-level module, the crate root, wildcards don't work. I can use either use {SomeSymbol, SomeOtherSymbol}; or use ::{SomeSymbol, SomeOtherSymbol}}; but neither use *; nor use ::*; work.
Why doesn't it work and how to import everything from the crate root?

As of Rust 1.14, use *; and use ::*; now work as intended (importing everything from the crate root)!
Useful links:
PR that introduced the syntax
Old issue about the lack of said syntax
My old answer in the edit log

Related

Will everything imported by a wildcard use directive ("use some_crate::*") be included in the binary?

If I import everything using * in Rust's use, does the built file include only what I use in the import?
e.g. use std::io::prelude::*
Through LLVM, Rust benefits from aggressive dead code elimination.
In fact, you can see that work by default: unless no_std, code implicitly has the standard prelude in-scope.
For instance, compare trivial printing code versus trivial printing code with a (useless) Box invocation: https://godbolt.org/z/3KWbGeqsh
Only the latter has the relevant Box code generated and compiled in.

What's the proper way to import mods in other files in Rust?

I am writing a Rust project and I want to use multiple mods. The src directory has files:main.rs, default.rs, difficult.rs.
The default.rs has:
pub struct Info {...}
pub fn f(...) {...}
and adding mod default, I can directly use default::Info and default::f.
But I want to use Info in difficult.rs and I add mod default into difficult.rs, then it gets error which says that default is not found.
How should I fix this?
How should I fix this?
You might want to read the book? The solution is at section 7.4. Rust is not a language which is easy to learn, and learning rust by osmosis or random walking tends to be a frustrating and unsuccessful endeavour.
I would very strongly recommend reading the book cover to cover at least once.
Anyway use is how you "import" things into the local scope.
mod is how you declare a module. If you use mod multiple times for the same file, you're essentially creating copies of the modules, which the compiler will see as two unrelated modules with unrelated types.

How do I find the path to the home directory for Linux?

I need the path contained in $HOME, the path to the home directory. The function home_dir seems to do exactly that but is deprecated. What should I use in its place?
The documentation says this :
Deprecated since 1.29.0:
This function's behavior is unexpected and probably not what you want. Consider using a crate from crates.io instead.
What type of crate should I use instead? Is there really no alternative in standard Rust?
From the home crate.
The definition of home_dir provided by the standard library is incorrect because it relies on the $HOME environment variable which has basically no meaning in Windows. This causes surprising situations where a Rust program will behave differently depending on whether it is run under a Unix emulation environment. Neither Cargo nor rustup use the standard libraries definition - instead they use the definition here.
There is discussion about bringing home_dir back into the standard library, but for now the home crate is probably your best option. It provides canonical definitions of home_dir, cargo_home, and rustup_home:
match home::home_dir() {
Some(path) => println!("{}", path.display()),
None => println!("Impossible to get your home dir!"),
}
From the dirs crate.
Note that the crate provides paths to many standard directories as defined by operating systems rules, not only the home directory:
Experience has shown that 90% of those asks for $HOME are better served by one of the other functions this crate provides.
If your requirements are more complex, e. g. computing cache, config, etc. paths for specific applications or projects, consider using the directories crate instead.

How to use structures from sibling/adjacent rust file?

I'm trying to edit a Rust project with the following structure:
src/iface/ip.rs
src/iface/tun.rs
I want to call things from ip.rs inside tun.rs. So on tun.rs I added:
use iface::ip;
but it says
unresolved import `iface::ip`
no `ip` in `iface`rustc(E0432)
This seems to be the way to do it as explained here https://stackoverflow.com/a/30687811/6655884 and here https://stackoverflow.com/a/26390046/6655884
I also tried mod ip but it didn't work either.
In iface/mod.rs
pub mod ip;
In iface/tun.rs
use crate::iface::ip;

Import a javascript module as dynamic using typescript

I want to import a normal javascript module (e.g. vhost) into my node.js typescript file using CommonJS.
I can do this with the following line:
import vhost = require('vhost')
We assume that I can't find a .d.ts file on the internet, but I also don't want to write it by myself, so I just use the vhost variable without intellisense.
The compiler complains and complains:
How can I tell that I just want it to be 'dynamic' (like the C# dynamic keyword or 'var' in normal javascript) and use all of the things in the picture above?
I could create a vhost.d.ts file, but I don't know what to write in there:
declare module 'vash' {
// what to write here?
}
I found this out while typing the question, it was so easy that it is almost embarrassing, but maybe somebody has this problem too.
Just use var instead of import:

Resources