23

I'm new to Rust and I want to build and run my project. I use something like:

cd %project_path%
cargo run

I want to be able to write cargo run -path %project_path% in a single line because I want to create a build script that doesn't allow changing the working directory. It seems that cargo doesn't have any -path or -target keys, which would define target directory, and I always get the message

could not find Cargo.toml in C:\WINDOWS\system32 or any parent directory

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
  • Maybe you want the `--manifest-path` option? (The `--help` argument will print the arguments and descriptions of them.) – huon Jan 31 '16 at 11:48
  • I want to be able to compile and run a project placed not in current working directory, but in path which I specify – Alex Zhukovskiy Jan 31 '16 at 18:46
  • In *nix shell terminology, you could use `(cd $project_path; cargo run)`: start a new subshell, in which you change directory and then execute the desired task. For Windows I think that’d be something vaguely along the lines of `cmd -c "cd %project_path%; cargo run"`. – Chris Morgan Jan 31 '16 at 22:20
  • 2
    @AlexZhukovskiy, did you try the `--manifest-path` option? – huon Feb 01 '16 at 01:15
  • @huon i was thinking that it's for metadata info and so on, but it has needed behaviour. Thanks. – Alex Zhukovskiy Feb 01 '16 at 08:33

1 Answers1

27

The --manifest-path path/to/Cargo.toml option to almost all cargo subcommands allows pointing it to a specific Cargo.toml file to use, overriding the default of searching the current directory and its parents for a file called Cargo.toml (this file is the "manifest").

Incidentally, unix-y commands usually take a -h or --help argument which prints information about their command line options, cargo and rustc are no exception. E.g.

$ cargo run --help
Run the main binary of the local package (src/main.rs)

Usage:
    cargo run [options] [--] [<args>...]

Options:
    -h, --help              Print this message
    --bin NAME              Name of the bin target to run
    --example NAME          Name of the example target to run
    -j N, --jobs N          The number of jobs to run in parallel
    --release               Build artifacts in release mode, with optimizations
    --features FEATURES     Space-separated list of features to also build
    --no-default-features   Do not build the `default` feature
    --target TRIPLE         Build for the target triple
    --manifest-path PATH    Path to the manifest to execute
    -v, --verbose           Use verbose output
    -q, --quiet             No output printed to stdout
    --color WHEN            Coloring: auto, always, never

If neither `--bin` nor `--example` are given, then if the project only has one
bin target it will be run. Otherwise `--bin` specifies the bin target to run,
and `--example` specifies the example target to run. At most one of `--bin` or
`--example` can be provided.

All of the trailing arguments are passed to the binary to run. If you're passing
arguments to both Cargo and the binary, the ones after `--` go to the binary,
huon
  • 94,605
  • 21
  • 231
  • 225
  • 4
    I know what `-h` command does but I didn't expect that `manifest to execute` is `cargo.toml`. Just a misunderstanding. I was thinking it's an additional metadata file (like application manifest in `.Net`), this is why I didn't use this key. Thanks for an answer. – Alex Zhukovskiy Feb 01 '16 at 17:50
  • The `--manifest-path` option worked only when the given file was explicitly named `Cargo.toml` for me. In my use case, I want to have two manifest files, one for Production and one for development environment. I have a makefile where I want to specify which manifest file to use based on the target. Has anyone done that? – theoctober19th Jun 30 '23 at 09:29