When should I use the --bin option for cargo new? - rust

What is the difference between cargo new <project_name> --bin and cargo new <project_name>?
It seems both commands make exactly the same project; all components are consistent.
I think --bin stands for "binary", but I don't know when to use this option or not.

There is no difference between cargo new and cargo new --bin. From First Steps with Cargo, emphasis mine:
To start a new package with Cargo, use cargo new:
$ cargo new hello_world
Cargo defaults to --bin to make a binary program. To make a library, we would pass --lib, instead.
Likewise, Cargo's command line help tells you the same thing. From cargo new --help, with some irrelevant lines removed:
% cargo new --help
OPTIONS:
--bin Use a binary (application) template [default]
--lib Use a library template
See also
Why does `cargo new` create a binary instead of a library?
What is the difference between library crates and normal crates in Rust?

Related

Difference between `cargo doc` and `cargo rustdoc`

According to doc.rust-lang.org
cargo rustdoc
build[s] a package's documentation, using specified custom flags
cargo doc
build[s] a package's documentation
What is the difference between the two? From what I understand cargo rustdoc is just like cargo doc, but it allows for more lints—for instance:
#![deny(rustdoc::broken_intra_doc_links)]
Is this correct? Oddly enough, cargo rustdoc will also fail in certain situations where cargo doc doesn't. For instance
some/folder on some-branch [$!] via 🦀 v1.60.0-nightly
❯ cargo doc
Finished dev [unoptimized + debuginfo] target(s) in 0.53s
some/folder on some-branch [$!] via 🦀 v1.60.0-nightly
❯ cargo rustdoc
error: manifest path `some/folder/Cargo.toml` is a virtual manifest, but this command requires running against an actual package in this workspace
Also, cargo doc does not support adding the -D option, whereas cargo rustdoc does.
❯ cargo doc -- -D rustdoc::broken_intra_doc_links
error: Found argument '-D' which wasn't expected, or isn't valid in this context
USAGE:
cargo doc [OPTIONS]
For more information try --help
Their relationship is like between cargo build and cargo rustc: cargo doc performs all the usual work, for an entire workspace, including dependencies (by default). cargo rustdoc allows you to pass flags directly to rustdoc, and only works for a single crate.
Here is the execution code for cargo rustdoc. Here is the code for cargo doc. The only differences is that cargo rustdoc always specify to not check dependencies while cargo doc allows you to choose (by default it does, but you can specify the flag --no-deps), and that cargo rustc allows you to pass flags directly to rustdoc with the flags after the --.

How to solve ”Blocking waiting for file lock on build directory“ problem?

I want to generate some .rs code in my rust program, and I create a package gen-proxy-ffi which use bindgen to do that. The following command can generate codes I need.
cargo run --package gen-proxy-ffi --bin gen-proxy-ffi
When the codes are generated, I can then build the whole project by
cargo build
Now, I want cargo build will automaticly generate code by calling cargo run --package gen-proxy-ffi --bin gen-proxy-ffi. I know I can write some code to build.rs to specify rust's compiling routine, so I write the following codes, where make-gen-proxy-ffi.sh encaplsules cargo run --package gen-proxy-ffi --bin gen-proxy-ffi.
// build.rs
use std::process::Command;
use std::io::Write;
fn main() {
let o = Command::new("sh").args(&["make-gen-proxy-ffi.sh"]).output().expect("sh exec error!");
}
However, when running cargo build now, it prints out the following error
+ cargo run --package gen-proxy-ffi --bin gen-proxy-ffi --
Blocking waiting for file lock on build directory
After some searching, I know it is because cargo build on the same project twice at one time.
However, from this post, I found no solution to this, so I wonder if there are any ways I can generate code and build the whole project by one cargo build?

Get the --bin argument in build.rs

I'd like to handle each binary differently in build.rs.
I can use env!("CARGO_PKG_VERSION") to get the version info from Cargo.toml.
Is there something similar for getting the bin argument?
I'd like to get binary_name when running cargo run --bin binary_name.

Where does the custom Cargo command "install-ra" in rust-analyzer come from?

The instructions to build rust-analyzer say to run this command:
cargo install-ra
It works, but how? It's not an official command and it isn't in cargo --list or Cargo.toml.
The repository in question contains a custom local Cargo configuration file in ".cargo/config". In this case, it defines a bunch of aliases, including install-ra:
[alias]
# Installs the visual studio code extension
install-ra = "run --package ra_tools --bin ra_tools -- install-ra"
install-code = "run --package ra_tools --bin ra_tools -- install-ra" # just an alias
See also:
Where can I find .cargo/config?

How can I optionally pass rustc flags depending on a Cargo feature?

The program I'm writing runs much faster when the -C target-cpu=native flag is passed to rustc. I want to give users a simple, platform-independent way to enable this when compiling, so I added a Cargo feature cpu_native = [] in Cargo.toml and created this Cargo config in my project:
[target.'cfg(cpu_native)']
rustflags = ["-C", "target-cpu=native"]
However, this has no effect on my program, and passing --features cpu_native to Cargo does not even trigger a recompile. Changing to the following Cargo config does force re-compilation with faster instructions:
[build]
rustflags = ["-C", "target-cpu=native"]
However, this will compile with target-cpu=native with the default Cargo features, which was not what I wanted. From the Cargo book, what I want seems to be possible, but I don't see what I'm doing wrong.
I don't think this is supported (yet?). I enhanced Cargo to print out what config flags are checked against when resolving:
[
Name("debug_assertions"),
Name("proc_macro"),
KeyPair("target_arch", "x86_64"),
KeyPair("target_endian", "little"),
KeyPair("target_env", ""),
KeyPair("target_family", "unix"),
KeyPair("target_os", "macos"),
KeyPair("target_pointer_width", "64"),
Name("unix"),
]
[target.'cfg(cpu_native)']
This is the incorrect syntax for a Cargo feature; it would normally be cfg(feature = "cpu_native").

Resources