Hi Rust forum I am new to programming

Hi all as mentioned above I am new to programming . I just picked it up as a hobby. I started with rust trying to make a crypto wallet . I having problems writing paths in my cargo.toml in vscode2 .
Identity={path=“::rust -libp2p-0.54.1::identity”,features =[“identity” , “peerId”]}
This is a file that’s already downloaded on my computer folder rust -libp2p is under my stc folder. Is this the correct form to get vs code to recognize the file.?Thanks for your help and time

Welcome to Rust! Cargo.toml manages package metadata for Cargo. Your problem looks like a misunderstanding about modules and packages.

Modules, e.g. ./src/somefile.rs can be imported from another file. If I was in my ./src/main.rs or ./src/lib.rs, I could use mod somefile; and then use somefile::some_function;. Packages, however, are not local. You can download them and add them to your Cargo.toml using Cargo. You should never use a module-like path to define package locations. It’s looking for a path, like ./package/.

Remove that line you added to your Cargo.toml, and run this command at your project root: cargo add libp2p. This will add the latest version of the package libp2p to your Cargo.toml, so that when you run or build it, (cargo run or cargo build) it will download and then compile it.

Also, the features you listed in the line aren’t what you think they are. Features are defined by the crate, normally to make some code or dependencies optional. identity and PeerId aren’t features of libp2p, they’re modules and structures respectively. A feature of libp2p would be async-std, because when you run cargo add libp2p —features async-std, it activates some code that doesn’t get compiled unless async-std is turned on.

If you want to use PeerId from libp2p, you would, wherever you want to use it, add use libp2p::identity::PeerId;. Note that you should not add mod libp2p;, because, since this isn’t a local file, like somefile.rs, Rust already knows where it is, and doesn’t need to be told to add it. I would also, since you’re new to programming, and in Rust nonetheless, read the book. It’s an amazing place to learn everything about Rust.

Good luck!

Thanks

The overloaded term "path" might be a bit confusing. In the cargo manifest, paths refer to file system paths. Rust module paths are unrelated to the file system layout and are oriented toward the module hierarchy.

I don't think the terminology overload is a bad, but the intuitive impression that the module hierarchy follows the file system is perhaps a bigger contributor to confusion in this area. [1]


  1. Slightly off-topic, but there was a recent conversation about the module hierarchy confusion on IRLO. It's a topic that comes up more often than ideal. It's a problem that appears simple on the surface but hides a great deal of incidental complexity. ↩︎

2 Likes