Newbie question - How to run program in Eclipse


#1

Hello all, I’m very very new to Rust. I have everything installed (I’m on Windows and in Eclipse). I’ve taken a very small piece of code from the internet and put in in a hello.rs file. It has the following content:

fn main() {
_ println!(“Hello World!”);_
}

I can press CTRL + B to do a build. My Eclipse Console now says:

==================== Starting Rust build ====================
************ Building Rust project: rust_hello_world ************
>> Running build with: C:\Users\Marcel van Langen.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe test --no-run --message-format=json
{“features”:[],“filenames”:[“D:\inetpub\wwwroot\rust_hello_world\target\debug\libhello_world.rlib”],“fresh”:true,“package_id”:“hello_world 0.1.0 (path+file:///D:/inetpub/wwwroot/rust_hello_world)”,“profile”:{“debug_assertions”:true,“debuginfo”:2,“opt_level”:“0”,“overflow_checks”:true,“test”:false},“reason”:“compiler-artifact”,“target”:{“crate_types”:[“lib”],“kind”:[“lib”],“name”:“hello_world”,“src_path”:“D:\inetpub\wwwroot\rust_hello_world\src\lib.rs”}}
_ {“features”:[],“filenames”:[“D:\inetpub\wwwroot\rust_hello_world\target\debug\hello_world-7373b76b3b7f04b4.exe”],“fresh”:true,“package_id”:“hello_world 0.1.0 (path+file:///D:/inetpub/wwwroot/rust_hello_world)”,“profile”:{“debug_assertions”:true,“debuginfo”:2,“opt_level”:“0”,“overflow_checks”:true,“test”:true},“reason”:“compiler-artifact”,“target”:{“crate_types”:[“lib”],“kind”:[“lib”],“name”:“hello_world”,“src_path”:“D:\inetpub\wwwroot\rust_hello_world\src\lib.rs”}}_
_ Finished dev [unoptimized + debuginfo] target(s) in 0.1 secs_
_ ^^^ Terminated, exit code: 0 ^^^_
************ Build terminated. ************

I think that’s OK. But now I don’t know how to run the program. I think I can use the Eclipse terminal, but I don’t know what the command is to run my program. Or do I have anything else wrong?

I hope you can help me get started on this!

Marcel


#2

Hey welcome to the Rust forum!

Unfortunately I am clueless about most IDEs including Eclipse.

But if you are happy with using the Eclipse terminal, you can run the following commands

Make debug build(fast build time, not optimized)

cargo build

Run debug build

cargo run

Test debug build

cargo test

Make release build(slow build time, optimized)

cargo build --release

Run release build

cargo run --release

Test release build

cargo test --release

You can find more info in the Rust book.

If you look at the output of Eclipse you posted
>> Running build with: C:\Users\Marcel van Langen.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe test --no-run --message-format=json

It is actually running cargo test, which invokes testing for the debug build.

In general, everything you need to do for a project can be done through cargo, from package management, project management, to crate publishing, and so on.


#3

If you are using the Eclipse plugin RustDT, it is no longer maintained.

You’ll probably have better luck with IntelliJ or VSCode.


#4

So, I could use CFEclipse, but it is better to do building, running etc. in a separate console. Unfortunately, I don’t understand yet how I can build and run (newby problems). I have my code on D:\projects\world\hello.rs. My cargo.exe is on C:\Users\Marcel…\bin\cargo.exe. To what directory should I go to, and what would be my command there? Many thanks in advance!


#5

No longer maintained, that’s too bad. I did not know that. It took my quite a lot of work to get it running. But I have VSCode as well, so I’ll start using that. Thanks for the tip!


#6

Look into adding the cargo.exe directory to your PATH variable(you will have to google the instructions for your specific windows version).

After that, you can invoke cargo in shell wherever you are. You may need to restart shell or logout and login before the PATH variable changes take place.

Once that’s out of the way, cd into your project directory(D:\projects\world\), and run the above commands(e.g. cargo run, cargo test). The project directory just refers to the directory you create when doing cargo init, or in your case, the folder that contains Cargo.toml created by Eclipse presumably.

Cargo.toml is basically your project config, and you can deal with basically every crate(Rust’s word for package) related matter via editing the file.


#7

I’m getting there with your help. I’ve followed the steps. Now I can build my little first project (with cargo build), but when I type ‘cargo run’, I get:

error: a bin target must be available for cargo run

I’ve done a bit of searching on the internet, but I’m not sure what to change. I know it has something to do with the cargo.toml file, but not sure what’s my next step here…


#8

I did however get it to work by typing:

rustc hello.rs
and then hello

but I’m pretty sure it’s not the same, since I can’t add dependencies etc. then.


#9

Ah, yes, so cargo by default initialises the project as a library project, thus nothing to be run.

I suspect the Eclipse addon only initialised it as library and thus the issue.

Just do

cargo init --bin helloworld

And you should see a folder named helloworld with Cargo.toml and a main.rs in src.

Just copy your old code back to the main.rs and you’re set!