Linked Questions
34 questions linked to/from Package with both a library and a binary?
9
votes
1
answer
6k
views
Renaming main.rs and using with cargo [duplicate]
Is it possible to change the name of main.rs and still have commands like cargo run work?
If it is possible could someone give me an example of how to have cargo run target say example.rs?
6
votes
1
answer
7k
views
File layout for multiple executables in a library crate [duplicate]
I'd like to create a rust package with two binary crates and a library which contains the shared code. I know how do do this for a simple program by putting the source files for the binaries in a src/...
8
votes
1
answer
2k
views
Creating a library and executable in a single project [duplicate]
How do I create a library and executable in one project? I just want to test my library while working on it and using tests isn't always the best way to do that. I believe I have to use [lib] and [???]...
5
votes
0
answers
2k
views
How to share code between binaries and a cdylib in a crate? [duplicate]
I'm building an app that hooks some Windows events.
The hook code must be in a DLL for that to work. I set [lib] with crate-type = ["cdylib"] in Cargo.toml and use it in main.rs.
│ Cargo.toml
│
└───...
0
votes
1
answer
527
views
Cargo is unable to find a crate [duplicate]
I have the following directory structure in my Rust project:
borrowing
|
|-------src
| |-------borrow.rs
| |-------lib.rs
|
|-------Cargo.toml
|-------main.rs
...
0
votes
0
answers
176
views
Accessing Rust modules from other directories fails. What is the issue? [duplicate]
Let's assume we have the following file tree:
src
|--bin
| |--hw.rs
|--libs
| |--streams.rs
| |--structs.rs
|--main.rs
I want to access some structs from the structs module inside the hw file, ...
204
votes
8
answers
149k
views
Rust modules confusion when there is main.rs and lib.rs
I have 4 files:
main.rs
mod bar;
fn main() {
let v = vec![1, 2, 3];
println!("Hello, world!");
}
lib.rs
pub mod foo;
pub mod bar;
foo.rs
pub fn say_foo() {
}
bar.rs
use crate::foo;
fn ...
279
votes
3
answers
141k
views
How can I build multiple binaries with Cargo?
I'd like to make a project with a daemon and a client, connecting through a unix socket.
A client and a daemon requires two binaries, so how do I tell Cargo to build two targets from two different ...
62
votes
5
answers
45k
views
How to move tests into a separate file for binaries in Rust's Cargo?
I created a new binary using Cargo:
cargo new my_binary --bin
A function in my_binary/src/main.rs can be used for a test:
fn function_from_main() {
println!("Test OK");
}
#[test]
fn my_test() {
...
35
votes
1
answer
9k
views
How can I use a module from outside the src folder in a binary project, such as for integration tests or benchmarks?
My project's path structure is as follows:
demo
├── benches
│ └── crypto_bench.rs
├── src
│ ├── main.rs
│ └── crypto.rs
├── Cargo.lock
└── Cargo.toml
crypto.rs contains a struct Crypto with ...
31
votes
1
answer
19k
views
How can I specify which crate `cargo run` runs by default in the root of a Cargo workspace?
Right now I have a Cargo workspace with three members.
[workspace]
members = [
"foo",
"bar",
"baz",
]
If I run cargo run in the root directory, I get this error:
error: manifest path /...
11
votes
1
answer
21k
views
Unresolved import when importing from a local crate with a main.rs file
I have included a library as a submodule in my program. The structure looks like this:
.
├── my_lib/
│ ├── Cargo.toml
│ └── src/
│ ├── lib/
│ │ ├── mod.rs
│ │ └── ...
1
vote
1
answer
5k
views
How do I tell Cargo to run files from a directory other than "src"?
I have a front-end project that has a lot of stuff in src folder, and I have the opportunity to also use Rust on the server side. All my Rust server files are in the server folder; how can I tell ...
1
vote
1
answer
4k
views
How to import a module from a directory outside of the `src` directory?
I'm stuck when learning how to access a module. I'm trying to insert a folder other than src into src. It's not working and it gives me an error. Here this is my project tree.
$ Project1
.
|-- src
| ...