Edition Differences : In the 2015 edition, crates in the ex tern prelude cannot be referenced via use declarations, so it is generally standard practice to include exterminate declarations to bring them into scope. Beginning in the 2018 edition, use declarations can reference crates in the ex tern prelude, so it is considered unidiomatic to use exterminate.
Note : Additional crates that ship with rust, such as allow, and test, are not automatically included with the -- ex tern flag when using Cargo. An external crate dependency can be declared without binding its name in scope by using an underscore with the form exterminate foo as _.
The no_link attribute may be specified on an exterminate item to prevent linking the crate into the output. I'm trying to work with the rust HTTP library, and I'd like to use it as the basis for a small project.
Shipmaster 253k3939 gold badges654654 silver badges877877 bronze badges You need to pass the -L flag to rust to add the directory which contains the compiled HTTP library to the search path.
Once you've built it, you can use the normal exterminate HTTP; in your code. The only trick is that you need to pass the appropriate -L flag to rust to tell it where to find lighttpd.
Note that this cargo package is a macro in my case. One is in conjunction with the crate keyword to make your Rust code aware of other Rust crates in your project, i.e., exterminate lazy_static;.
The first is in the form of external blocks, for declaring function interfaces that Rust code can call foreign code by. Working with non- Rust languages and FFI is inherently unsafe, so wrappers are usually built around C APIs.
The mirror use case of FFI is also done via the ex tern keyword: The module system is often one of the hardest things for people new to Rust.
Everyone has their own things that take time to master, of course, but there's a root cause for why it's so confusing to many: while there are simple and consistent rules defining the module system, their consequences can feel inconsistent, counterintuitive and mysterious. As such, the 2018 edition of Rust introduces a few new module system features, but they end up simplifying the module system, to make it more clear as to what is going on.
Paths may start with a crate name, even within submodules. These may seem like arbitrary new rules when put this way, but the mental model is now significantly simplified overall.
This one is quite straightforward: you no longer need to write exterminate to import a crate into your project. One small note here: cargo fix will not currently automate this change.
Some examples of needing to explicitly import root crates are: For example, some internal crates used by the standard library itself need this.
Proc_macro : This is automatically imported by Cargo if it is a proc-macro crate starting in 1.42. Extern crate proc_macro; would be needed if you want to support older releases, or if using another build tool that does not pass the appropriate -- ex tern flags to rust. Test : This is only available on the nightly channel, and is usually only used for the unstable benchmark support.
Then removing the exterminate line on its own won't work. For instance, ::foo::bar always refers to the name bar inside the external crate foo.
Previously, using an external crate in a module without a use import required a leading :: on the path. Now, exterminate names are in scope in the entire crate, including submodules.
In Rust 2018 the restriction that a module with submodules must be named mod.rs is lifted. In Rust 2015, paths work differently in use declarations than they do elsewhere.
Those differences didn't have any effect in the top-level module, which meant that everything would seem straightforward until working on a project large enough to have submodules. In Rust 2018, paths in use declarations and in other code work the same way, both in the top-level module and in any submodule.
Will look exactly the same in Rust 2018, except that you can delete the exterminate line: If a path is ambiguous, such as if you have an external crate and a local module or item with the same name, you'll get an error, and you'll need to either rename one of the conflicting names or explicitly disambiguate the path.
To explicitly disambiguate a path, use ::name for an external crate name, or self::name for a local module or item. Making statements based on opinion; back them up with references or personal experience.
Since this string does not have any operators in it, it is interpreted the same way as if we had specified “^0.1.12”, which is called a caret requirement. Caret requirements allow Server compatible updates to a specified version.
An update is allowed if the new version number does not modify the left-most non-zero digit in the major, minor, patch grouping. If instead we had specified the version string as ^1.0, cargo should update to 1.1 if it is the latest 1.y release, but not 2.0.
Tilde requirements specify a minimal version with some ability to update. As shown in the examples above, multiple version requirements can be separated with a comma, e.g., >= 1.2, < 1.5.
This tells Cargo that we depend on a crate called hello_utils which is found in the hello_utils folder (relative to the Cargo.Tom it’s written in). One example where this can be useful is when you have split up a library into multiple packages within the same workspace.
Platform-specific dependencies take the same format, but are listed under a target section. Like with Rust, the syntax here supports the not, any, and all operators to combine various CFG name/value pairs.
If you want to know which CFG targets are available on your platform, run rust c--print=CFG from the command line. If you want to know which CFG targets are available for another platform, such as 64-bit Windows, run rust c--print=cf g--target=x86_64-pc-windows-msvc.
Note : Custom target specifications are not usable on the stable channel. For most use cases, dev-dependencies are not needed when published, though some users (like OS packagers) may want to run tests within a crate, so providing a version if possible can still be beneficial.
In this case, the dependency will only be built when the host platform matches the specified target. A package itself and its build script are built separately, so their dependencies need not coincide.
If a package you depend on offers conditional features, you can specify which to use : When writing a section in Cargo.Tom the key you write for a dependency typically matches up to the name of the crate you import from in the code.
For some projects, though, you may wish to reference the crate with a different name in the code regardless of how it's published on crates.Io. Avoid the need to use foo as bar in Rust source.