Help or Guide with Rust Book? (hello cargo/file locations?

Hi,

I am learning Rust and new to coding in general. I am wondering about hello cargo in rust book?

I am unable to recall Hello world by using command cargo build in my default terminal. I am only able to do it by cd the source path folder where my rust code is saved. Is this supposed to happen? The book reads like I should be able to recall the code from the terminal withoout selecting the source path fro rust files?

What am I doing wrong?

PS C:\Users\CC> cargo build
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
PS C:\Users\CC> cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target\debug\C.exe`
Hello, world!
PS C:\Users\CC> .\target\debug\hello_cargo.exe
.\target\debug\hello_cargo.exe : The term '.\target\debug\hello_cargo.exe' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At line:1 char:1
+ .\target\debug\hello_cargo.exe
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\target\debug\hello_cargo.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Looks like the name is C and not hello_cargo.

C:\Users\##> .\target\debug\hello_cargo.exe
.\target\debug\hello_cargo.exe : The term '.\target\debug\hello_cargo.exe' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At line:1 char:1
+ .\target\debug\hello_cargo.exe
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\target\debug\hello_cargo.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

also getting these returns, looks good but the target debug isnt

PS C:\Users\##> cargo build
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
PS C:\Users\##> cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target\debug\C.exe`
Hello, world!
PS C:\Users\##> cargo check
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s

Your program has the name ā€œCā€. Therefore, target\debug\C.exe will work to run it and target\debug\hello_cargo.exe will not work.

If you want to change the name, open Cargo.toml and change the name = "C" line to name = "hello_cargo" (or whatever else you want).

1 Like

I did that, but it still returns error...

PS C:\Users\C> .\cargo.toml
PS C:\Users\C>
PS C:\Users\C> target\debug\hello_cargo.exe
target\debug\hello_cargo.exe : The module 'target' could not be loaded. For more information, run 'Import-Module
target'.
At line:1 char:1
+ target\debug\hello_cargo.exe
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (target\debug\hello_cargo.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoLoadModule

EDIT: Also why cnt i run my code in MS VS 2022? I am having to run everything in powershell? I downloaded all the extensions I needed im pretty sure

You must not have understood what they said:

As they said, try running:

PS C:\Users\C> target\debug\C.exe
1 Like
PS C:\Users\C> cargo run
   Compiling hello_cargo v0.1.0 (C:\Users\C)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.56s
     Running `target\debug\hello_cargo.exe`
Hello, world!
PS C:\Users\C> cargo build
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
PS C:\Users\C> target\debug\hello_cargo.exe
Hello, world!

I think I fixed it?...I wanted to recall hello_cargo not \C

Yes. I assume you did what @kpreid recommended:

If you want to change the name, open Cargo.toml and change the name = "C" line to name = "hello_cargo" (or whatever else you want).

1 Like

I suggest checking @kpreid's reply as the solution since they provided it.

2 Likes