Working with powershell ise


#1

hi, I installed rust in windows by following “the book”. but when i try to compile the hello world program it shows

PS C:\Users\Katsukage> fn main.exe() {
println!(“Hello, world!”);
}
At line:1 char:13

  • fn main.exe() {
  • ~
    An expression was expected after ‘(’.
  • CategoryInfo : ParserError: (:slight_smile: [], ParentContainsErrorRecordException
  • FullyQualifiedErrorId : ExpectedExpression

I have basic knowledge of c and c++ only. what am i doing wrong?
im using windows 10, in powershell ISE
i also checked the path as mentioned above it is correct.
also installed vc++ , followed the default installation options.


#2

Powershell ISE only supports the language Powershell. (At least, as far as I am aware - are you able to run C and C++ code directly in Powershell?).

A more typical workflow for C, C++, and Rust is to use a general-purpose editor to create source files, then use a command line (possibly Powershell) to run the compiler and the executable.

Note that Powershell ISE is not a general-purpose editor. It only allows you to create Powershell .ps1 files.

It may be worth reading the page at http://www.jonathanturner.org/2017/03/rust-in-windows.html to get you started.


#3

Yes, @jongiddy is correct. I use PowerShell for my day-to-day Rust development, but use Visual Studio: Code to edit the text, and run cargo build inside of PS directly.

.ps1 files are PowerShell specific files, not Rust files!


#4

so im extremely confused right now. I have installed rust,vc++ and visual rust. where do i write the hello world code?? also when i try double clicking the rust,rustc,cargo… application files nothing seems to happen. what am i doing wrong?


#5

Can you type rustc at the powershell prompt, does it find rustc? if so, you can type: rustc myfile.rs and it will compile and run this specific file. Beyond that, you really want to work within a project. Start here

NOTE: Powershell ISE is different than Powershell, don’t use ISE for this purpose.


#6

Bro first of all thanks for your suggestion, I located the file and tried
compiling it, but nothing happened. Now the folder containing the .rs file
has two more files one .exe and one .pdb, I’m double clicking on the .exe
also nothing is happening. What do you suggest?


#7

Also i tried rustc filename.exe but powershell returned couldn’t read
"hello.exe": stream did not contain valid UTF-8


#8

hello.rs is the “source code file”. You want to write it in a code editor, then use rustc to convert it into an “executable file.” The command looks something like this (“PS>” represents the powershell prompt).

PS> rustc hello.rs

When it’s done running, a file called “hello.exe” will pop into existence (exe stands for executable file). That file is a program, just like rustc itself is a program, that you can run directly (the line without a prompt on it represents the output that hello.exe pushes to the screen):

PS> ./hello.exe
Helllo world!

Since hello.exe has already been converted into an executable program, you don’t need to do it again (as you’ve noticed, rustc errors out because executable programs are not valid source code).


#9

Thanks for your comment, I got it working and managed to run hello world :blush:

How do i move back to
C:\users\katsukage\projects from C:\users\katsukage\projects\hello_world
in Windows powershell

A post on stack overflow suggests using push-location and pop-location .
Push-location is working and pushing any directory i want. But pop-location
is not working, dunno why? any suggestions?


#10

Use the cd (change directory) command for this.
There are two “special” directory names:

  • . (period) means the current directory
  • .. (double period) means the parent directory.

The command

cd ..

will therefore get you back to the parent directory.

Alternatively, the command

cd C:\users\katsukage\projects

will have the same effect.

It looks like you are trying to learn many things at the same time. It might be better to first get yourself accustomed with the basic PowerShell usage (and in general command line interfaces) first.


#11

My main aim is to learn rust, but to navigate to directories, run,compile and to use cargo i need powershell, hence the doubts. It only seems fair that I learn basic powershell first. Thanks again brother :raised_hands: