Questions tagged [rust-crates]

Crates are the unit of compilation in Rust.

Crates are the unit of compilation in , and the basic building block of its system.

The crates can be published in crates.io, the official Rust packages registry, where you can browse them.

206 questions
3
votes
1 answer

UseDeclaration cannot find struct in the crate root

I start with the cargo new tst. Then in the src/lib.rs I have: pub struct Config {} And src/main.rs looks like the following: use crate::Config; fn main() {} This however does not compile: > cargo run Compiling tst v0.1.0…
NarūnasK
  • 4,564
  • 8
  • 50
  • 76
3
votes
1 answer

How can I use a trait implementation from another crate in the original crate?

I have a Rust project which divided among several crates in a workspace. One of these crates is a test crate which holds utilities for use in unit and integration tests within the other crates. In one crate I define a trait that is implemented by a…
Wil
  • 115
  • 6
3
votes
1 answer

Can I publish a crate that uses a patch?

The section in the Rust documentation that talks about patching explains how to implement a patch, but all of the examples it uses are for "testing" and short term fixing. What I want to do is make a patch for one of the crates I depend on that…
Migwell
  • 18,631
  • 21
  • 91
  • 160
3
votes
1 answer

Why can't Rust find the "js_sys" crate?

I'm new to rust. I'm trying to use the crate js_sys which contains a Math::log. I have included js_sys = 0.3.48 as the crate website tells me, and then use js_sys::Math::log; in main.rs. I get an error that rust cannot find the crate. Steps to…
user11391572
3
votes
3 answers

Resolving imports in Rust

I'm having trouble with importing rand crate from crates.io. After adding the line rand="0.8.3" and then running command cargo build for the project, it keeps displaying the same errors: error[E0432]: unresolved import `rand` --> main.rs:1:5 | 1…
Nantarand
  • 51
  • 1
  • 1
  • 4
3
votes
1 answer

Can we download something & set environment variable during crate installation?

As a Rust driver crate developer, I would like to perform below steps during my crate installation/download when used by any other Rust program: Check the platform i.e. Windows or UNIX or macOS. Download the corresponding platform-specific binary…
3
votes
1 answer

Where is my private code exposed as public?

I found out that I have some dead code in my code base but a not getting a dead code warning as expected. I read the Visibility and Privacy article from the rust book. I am following the example on creating a "helper module" with code to be used in…
cambunctious
  • 8,391
  • 5
  • 34
  • 53
3
votes
2 answers

How to add external packages and run in rust compiler?

I am compiling and building an example program using rust. I chose rustc instead of cargo for compiling because it being a simple personal test project. So far using rustc for compiling and building executable worked fine but when I tried to add an…
Eka
  • 14,170
  • 38
  • 128
  • 212
3
votes
1 answer

How to reuse code from the main bin in another bin?

My project structure looks like this: . ├── Cargo.lock ├── Cargo.toml └── src ├── bin │   └── other.rs ├── main.rs └── util.rs (code: https://gitlab.com/msrd0/cargo-bin-import) In my other.rs, I'm trying to reuse code from the util…
msrd0
  • 7,816
  • 9
  • 47
  • 82
3
votes
1 answer

How do I add a constructor to an existing primitive type?

I am trying to make primitive types and object types by adding the new method to usize: impl usize { fn new(value: &u32) -> usize { value as usize } } I have no idea about what the message tries to say: error[E0390]: only a single…
khanh
  • 600
  • 5
  • 20
3
votes
1 answer

Importing non-root module from multiple non-root binaries

I am learning Rust and decided to write a simple client/server program. Both the client and the server will be using a very simple module I've already written. Knowing that this code might grow, I decided to compartmentalize my source for clarity.…
soplu
  • 145
  • 1
  • 8
3
votes
0 answers

"Found staticlib instead of rlib or dylib" when compiling using Visual Studio

I'm using the image crate and Visual Studio with Rust plugin. The code is like this: extern crate image; When I use the command line cargo build, everything was fine. With the Visual Studio build I get src\main.rs(4,1): error : found staticlib…
bitnick
  • 1,903
  • 3
  • 16
  • 22
2
votes
1 answer

Publish only the binary crate of a workspace

I have a project in the form of a Rust workspace with a dozen of crates, including only one binary crate, that I want to publish on crates.io. I'd like to not publish the library crates that are only here as an attempt to separate concerns and…
Quessours
  • 47
  • 5
2
votes
1 answer

How to fix "updating crates.io index failed" problem when I build the Rust project

I have a problem building the Rust project. I have changed the Cargo.toml like the following: [package] name = "here" version = "0.1.0" edition = "2021" [dependencies] rand="0.8.5" Only added rand to dependencies. But, when I build the project,…
multi422
  • 61
  • 1
  • 10
2
votes
2 answers

How to tell the version number of the Rust crate I'm in?

Is there a robust way, maybe something in cargo CLI, to get the version of the crate? I can grep on Cargo.toml, but I'm looking for something that won't break in 6 months. Is there a better way?
SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85