How to build or run a specific Rust binary project in Atom? - rust

I am using multiple main files using a single Cargo.toml:
[package]
name = "rust_example"
version = "0.1.0"
authors = [""]
[dependencies]
[[bin]]
name = "main"
path = "src/main.rs"
[[bin]]
name = "fibonachi"
path = "src/fibonachi/main.rs"
I tested on console
cargo run --bin main` and `cargo run --bin fibonachi
And it compiles correctly.
I installed Atom along with several plugins (build, build-cargo, busy, language-rust, linter). When I compile with Atom, it shows these lines:
error: `cargo run` requires that a project only have one executable; use the `--bin` option to specify which one to run
error: `cargo run` requires that a project only have one executable; use the `--bin` option to specify which one to run
I can't find how to specify which binary I'd like to build from within Atom.

Related

Empty rust file creates massive WASM build

Building the following rust file is producing a binary of 720KB.
I would expect a virtually empty build, what am I missing here? Is the full core libary getting included somehow?
Here's the code
#![no_std]
#[panic_handler]
fn handle_panic(_: &core::panic::PanicInfo) -> ! {
unreachable!()
}
And the cargo.toml
[package]
name = "wasm_test"
version = "0.0.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[profile.release]
opt-level = 's'
lto = true
And the command I'm using to build:
cargo build --target wasm32-unknown-unknown
Ok turns out I had two problems:
I was building this as a crate in a workspace, and that apparently ignores crate specific profiles
When i copied the crate out to mess around with it and isolate the problem, i was forgetting to add the --release flag, thanks #isaactfa.
In other cases i have received this warning.
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
for some reason I wasn't getting it when doing the workspace builds.
I've added the crate to the workspace exclude list and am building it seperately, and its compiling to a far more appropriate 411 bytes, down from 727151 bytes.

How to disable lints for the "bin" target only [duplicate]

This question already has answers here:
How to disable unused code warnings in Rust?
(11 answers)
Closed 3 years ago.
I have a Rust project folder structure that contains an executable and a shared C-compatible library that are both build using the same sources. The Cargo.toml manifest file looks like:
[package]
name = "foo-bar"
version = "0.1.0"
authors = ...
[lib]
name = "foo_bar"
crate-type = ["rlib", "cdylib"]
[[bin]]
name = "foo-bar"
test = false
doc = false
[dependencies]
...
As the executable is not using all of the code I get some "unused code" warnings when building the project with cargo build. I could add #[allow(dead_code)] lints all over my source code where necessary but that would disable them also when building the library target.
Is there a way to globally disable the "dead_code" lint only when compiling the (feature-wise smaller) bin executable target but having it enabled for the lib target?
You can modify a lint for a whole crate by putting an attribute with #! at the beginning of the crate:
main.rs:
#![allow(dead_code)]
// etc.

Specify build file for an example

I have a build script that is unnecessary for the library but required to build some examples. How do I specify a build file for an example with Cargo? I tried the following:
[[example]]
name = "basic"
build = "examples/build.rs"
but Cargo tells me the build key is unused.

Build only `lib` target

I want to build a dynamic link library (dll).
My Cargo.toml currently looks like this:
[package]
name = "sample"
version = "0.1.0"
authors = ["author"]
[lib]
name = "main"
crate-type = ["dylib"]
[dependencies]
I use VS Code with the RustyCode plugin as my IDE on windows.
When I run the build command this builds into a sample.exe and main.dll.
I know I can run cargo build --lib to only build my lib target but I dont have access to this command inside VS Code (afaik).
Is there anyway to specify that I only want to build the lib target in my Cargo.toml file so I can use the VS Code build command which runs cargo build/cargo run?
Cargo builds files using convention over configuration approach. When it finds a main.rs it builds an executable, and when it encounters lib.rs it expects to build a library.
Calling your lib main managed to confuse Cargo. The only solution I managed to find is to either change name of your crate from name = "main" to name = "foo" (and then rename your main.rs into foo.rs) or to change its name to lib.rs, as you did.
Just figured it: Rename the src/main.rs to src/lib.rs and it only builds the lib target!

capnpc::compile not writing files

I'm having difficulty working with the capnpc crate. I'm running Arch Linux and have installed capnp from the AUR and compiled capnpc-rust from the github project and put it in /usr/local/bin. I can manually compile the .capnp file easily with the command
capnp compile -orust --src-prefix=capnp capnp/message.capnp
I tried cloning the capnpc project and compiling the .capnp test file in the test directory and that didn't work either. I'm not getting any errors (whereas earlier I was getting "File not found") so it seems like capnpc is working, but I can't find files anywhere.
build.rs
extern crate capnpc;
fn main() {
::capnpc::compile("capnp", &["capnp/message.capnp"]).unwrap();
}
Cargo.toml
...
build = "build.rs"
[lib]
name = "rustp2p"
path = "src/lib.rs"
[build-dependencies]
capnpc = "*"
[dependencies]
capnp = "0.5.0"
Edit: .rs file builds out into /target/debug/build/.../out.
When you invoke capnpc::compile from a Cargo build script, the generated code goes in a subdirectory of target/ that can be found at main compile time through the OUT_DIR environment variable. This strategy is described in the Cargo documentation.
You shouldn't need to install the capnpc-rust binary in /usr/local/bin or anywhere else for this to work.
Your build.rs and Cargo.toml files look fine to me.
You might find it helpful to consult the addressbook example.

Resources