DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+2 votes

I've seen there are repositories on github where the package names differ from the folder name, and I would like to do that but I'm getting an error.

mkdir project

cd project

Create Cargo.toml

[workspace]

members = [
    "adder",
]

Create binary crate

cargo new adder

Add another package

[workspace]

members = [
    "adder",
    "add-one",
]

Generate library crate

cargo new add-one --lib

Change add-one/Cargo.toml name to something like myproject-add-one.

In the add-one/src/lib.rs file, let’s add an add_one function:

pub fn add_one(x: i32) -> i32 {
    x + 1
}

Add a path dependency on add-one to adder/Cargo.toml:

[dependencies]
add-one = { path = "../add-one" }

Filename: adder/src/main.rs

use add_one;

fn main() {
    let num = 10;
    println!(
        "Hello, world! {} plus one is {}!",
        num,
        add_one::add_one(num)
    );
}

When I compile I get an error:

error: no matching package named `add-one` found
location searched: /home/user/sync/code/rust/project/add-one
required by package `adder v0.1.0 (/home/user/sync/code/rust/project/adder)`

How to give a package a different name than it's folder?

by
edited by
0

Can you link to any example repository?

0

I've just copied the example from the docs. Haven't uploaded it anywhere yet.

0

I was actually referring to "I've seen there are repositories on github [...]" I was curious to see any of those.

0

The xi-editor was one of them.

0

Thank you for sharing the link. I've updated my answer to include this example as well.

1 Answer

+1 vote
 
Best answer

The path dependency add-one = { path = "../add-one" } means that you're dependent on a crate called add-one which is found in ../add-one. Therefore if you're going to change the library name to myproject-add-one you will need to define the dependency like this:

adder/Cargo.toml
----------------
[dependencies]
myproject-add-one = { path = "../add-one" }

and change your main.rs code accordingly in order to use the new name (use myproject_add_one;).

A second option is to specify additional dependency features using a different notation like this:

add-one/Cargo.toml
----------------
name = "myproject-add-one"

adder/Cargo.toml
----------------
[dependencies]
# empty

[dependencies.myproject-add-one]
path = "../add-one"

However, I think what you're looking for in this case is not changing the name of the package, but the output target of your add-one library. If this is it, then add the following to your add-one/Cargo.toml (see also "configuring a target"):

[lib]
name = "foobar"
by
edited by
Contributions licensed under CC0
...