DEV Community

Konstantin
Konstantin

Posted on • Originally published at iamkonstantin.eu

3 3

Adding an executable target to a Rust library

Using Cargo, we can easily create a library project with the new command:

cargo new pumpkin-rs --lib
Enter fullscreen mode Exit fullscreen mode

Of course, the primary purpose of libraries is to encapsulate some kind of functionality that can be then exported for use by other libraries or applications.

For the purpose of this post, let’s define a simple function in src/lib.rs which will be the essence of our library. Later we will see how we can use this function from a runnable binary target in the same project.

pub fn say_hello() {
    println!("Hello from the library");
}
Enter fullscreen mode Exit fullscreen mode

You may run cargo build just to double-check everything builds successfully… and voilà!

Cargo Targets

In certain situations, it may be helpful to have a library project include a runnable component - a stand-alone, runnable program that can be executed.

Cargo targets allow us to define different combinations of source files which can be compiled in a project. Targets can specify what part of the source code is a library, binary, example, unit test, or benchmark.

The cool thing is that Cargo also uses folder structure conventions to guess which source file does what. For example, src/lib.rs is where it will look for the library code, executable code is expected in src/main.rs or src/bin/.

Add a default binary target

Following the conventions above, we can add a default executable to a library project by creating src/bin/pumpkin.rs:

use pumpkin_rs::say_hello;

fn main() {
    say_hello()
}
Enter fullscreen mode Exit fullscreen mode

Running cargo run will execute the binary! 🎉

Not only that, but cargo build now also produces 2 separate outputs - we get the compiled binary for the library as well as the executable.

More than one binary targets

We are not limited to a single binary target. There can be more than one!
Let’s create src/bin/tomato.rs

fn main() {
    println!("Hello 🍅")
}
Enter fullscreen mode Exit fullscreen mode

Now, if we just run cargo run like before, we’d get an error message because there are two available binary targets, and cargo doesn’t know which one we want to use. The error message helpfully suggests we can use the target configuration in Cargo.toml to define a default-run key or pass the --bin argument to the run command.

cargo run --bin tomato
Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay