Why is my output of program different when using cargo run

Hi folks I just started using Rust and I am from a C an C++ background.

It may be a silly thing, but wanted to get some eyes on this issue or clarification.

Query: When I run my executable with cargo run I get a different output of my program than what I get when I run the rust executable manually.

== program and log =====

$ pwd
/home/rm/IdeaProjects/rust-dive-1 << Created a project from IntelliJ IDE

ls Cargo.lock Cargo.toml hw hw.rs rust-dive-1.iml src target

$ cat hw.rs
/* Hello program */
fn main()
println!("Hello {} Good to see you in {} ","Love", "Office");

cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs Running `target/debug/rust-dive-1` Hello, world! << Where is this output coming from ??

rustc hw.rs
./hw Hello Love Good to have you in Office << Expected output

$ cargo test
Compiling rust-dive-1 v0.1.0 (file:///home/rm/IdeaProjects/rust-dive-1)
Finished dev [unoptimized + debuginfo] target(s) in 0.36 secs
Running target/debug/deps/rust_dive_1-973a61f4c574e317

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
Running target/debug/rust-dive-1
Hello, world! << Where is this output coming from ?

Do you have a src/main.rs? I bet you do.

We also need to see your Cargo.toml file to get the complete picture, but assuming a default configuration: running cargo run compile src/main.rs and then run it. But in your example, you're compiling and running hw.rs

This may be a good time to review the cargo guide again: https://doc.rust-lang.org/cargo/getting-started/

1 Like

A minor note; to improve readability, please surround your code with backticks:

your code here

This will improve formatting.

eminence thanks for response. Oh yes there is a src/main.rs file and it does contain the printf . So why is cargo test and cargo run still picking up this while the original file has changed. May be I need to read the documentation.. (fyi :slight_smile: just getting started with rustc )

$ ls src/

$ cat src/main.rs
fn main() {
println!("Hello, world!");

And my Cargo.toml file looks like

$ cat Cargo.toml
name = "rust-dive-1"
version = "0.1.0"
authors = ["rhm x^^^^y@yyy.com"]

@OptimisticPeach did you mean to enclose the code with the 3 tildas. I got an compilation error. Missing something here?

/* Hello program */

fn main()
    println!("Hello {} Good to have you in {} ","Love", "Office");

$ rustc hw.rs
error: unknown start of token: `
--> hw.rs:3:1
3 | ```
| ^

@OptimisticPeach OH my bad.. It took me a minute to realize its markdown.. you meant for code highlighting in the comment webpage.. ( lol.. my bad.. I checked the rust comments syntax doc and then came back here to realize it was markdown u talking about :slight_smile: )
thanks. yeah sure.

Err, yes, this forum runs on Discourse, so it has built in markdown, in this case it's tailored for rust code and will do it by default.
To solve your problem though, the rust file hierarchy is very important;

    main.rs <-- entrypoint for program
mod my_module;
mod my_module_2;
fn main() {
    println!("This is the entry point!");

I'd read the docs though, it seems that sometimes the structure can be difficult to understand at the start, but trust me it is very straightforward:
A module can own items (functions, structs, etc.) It can also own another module. It can own this module inline:

mod foo {
    fn bar() {}

Or it can declare it as being in another file:

  • With a file of the module's name adjacent to the current file if the first file is called mod.rs, main.rs, or lib.rs:
    mod foo;
  • By having a folder which contains a mod.rs and is named after the module:
    main.rs is identical to the previous one
    This allows you to have a module which owns more modules
  • Your module's file is named, but has a folder with the same name adjacent to it, the items in which it can own:
    main.rs is identical to the first two
    mod bar;

@OptimisticPeach thanks for the details. Its nice modular programming approach.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.