Difference between revisions of "Rust-Programming-Language/C2/Hello-World-using-Cargo/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 4: Line 4:
 
|| '''Narration'''
 
|| '''Narration'''
 
|-
 
|-
|| '''Show Slide: '''
+
|| <div style="color:#000000;">Show Slide: </div>
  
'''Title Slide'''
+
<div style="color:#000000;">'''Title Slide'''</div>
  
|| Welcome to the Spoken tutorial on '''Hello World using Cargo'''.
+
|| <span style="color:#000000;">Welcome to the Spoken tutorial on </span><span style="color:#000000;">'''Hello World using Cargo.'''</span>
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| '''Show Slide: '''
+
|| <div style="color:#000000;">Show Slide: </div>
  
'''Learning Objectives'''
+
<div style="color:#000000;">'''Learning Objectives'''</div>
 
|| In this tutorial, we will learn how to
 
|| In this tutorial, we will learn how to
* Create a cargo project
+
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Create a cargo project</div>
* Write a Hello world rust program
+
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Write a Hello world rust program</div>
* Compile and Run the cargo project
+
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Compile and Run the </span>cargo project</div>
  
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| '''Show Slide: '''
+
|| <div style="color:#000000;">Show Slide:</div>
  
'''System Requirements'''
+
<div style="color:#000000;">'''System Requirements'''</div>
|| This tutorial is recorded using,
+
|| T<span style="color:#000000;">his tutorial</span> is recorded <span style="color:#000000;">using</span>
* Ubuntu Linux OS version 22.04
+
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Ubuntu Linux OS version 22.04</div>
* Rust version 1.80.0  
+
* <div style="margin-left:1.27cm;margin-right:0cm;">Rust version 1.80.0 </div>
* Visual studio code version 1.19.0
+
* <div style="margin-left:1.27cm;margin-right:0cm;">Visual studio code version 1.19.0</div>
  
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| '''Show Slide: '''
+
|| Show slide:
  
'''Prerequisites'''
+
Prerequisite
 
|| To follow this tutorial,
 
|| To follow this tutorial,
* You must have basic knowledge of any programming language
+
* <div style="margin-left:1.27cm;margin-right:0cm;">You must have basic knowledge of any programming language</div>
* You should also have '''Rust '''installed on your system.
+
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="background-color:#ffffff;">You should also have </span><span style="background-color:#ffffff;">'''Rust '''</span><span style="background-color:#ffffff;">installed on your system.</span></div>
* If not, go through the relevant tutorials on this website.
+
  
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| '''Show Slide: '''
+
|| <div style="color:#000000;">'''&nbsp;Slide: '''</div>
  
'''About Cargo'''
+
<div style="color:#000000;">'''About Cargo'''</div>
  
 
||  
 
||  
* '''Cargo '''is a '''Rust build system''' and '''package manager'''
+
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Cargo '''</span><span style="color:#000000;">is a </span><span style="color:#000000;">'''Rust build system'''</span><span style="color:#000000;"> and </span><span style="color:#000000;">'''package manager'''</span></div>
* Cargo handles the compilation process and automates the building of your project.
+
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Cargo handles the compilation process</span> and automates<span style="color:#000000;"> the building of </span>your project.</div>
* Cargo allows you to add and manage external libraries to your project
+
* <div style="margin-left:1.27cm;margin-right:0cm;">Cargo allows you at add and manage external libraries</div>
* In the '''Cargo.toml '''file, you can list your dependencies.
+
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">In the Cargo.toml</span><span style="color:#000000;">''' '''</span><span style="color:#000000;">file, you can list your dependencies.</span></div>
* Complex '''Rust '''programs need these '''dependencies''' to develop '''Rust projects'''
+
* <div style="margin-left:1.27cm;margin-right:0cm;">Complex '''Rust '''programs need these '''dependencies''' to develop '''Rust projects'''<span style="color:#252525;">.</span></div>
|-
+
|| '''Show Slide:'''
+
  
'''Cargo commands'''
+
|- style="border:1pt solid #252525;padding:0cm;"
|| Let’s begin understanding the commands first.
+
|| <div style="color:#000000;">Open Visual code editor</div>
* To create a Cargo project use '''cargo new project name'''
+
* To compile, use '''cargo build''' inside the created project.
+
* To run, use '''cargo run''' inside the project
+
  
|-
+
|| <span style="color:#000000;">Let us open the </span><span style="color:#000000;">'''Visual Studio Code editor.'''</span>
|| Open '''Visual code editor'''.
+
|- style="border:1pt solid #252525;padding:0cm;"
|| Let us open the '''Visual Studio Code editor.'''
+
|| <span style="color:#000000;">click on </span><span style="color:#000000;">'''terminal'''</span><span style="color:#000000;"> </span><span style="color:#000000;">and select</span><span style="color:#000000;"> </span><span style="color:#000000;">'''New Terminal.'''</span>
|-
+
|| <span style="color:#000000;">In the menu bar, click on </span><span style="color:#000000;">'''terminal'''</span><span style="color:#000000;"> </span><span style="color:#000000;">and select</span><span style="color:#000000;"> </span><span style="color:#000000;">'''New Terminal.'''</span>
|| Click on '''terminal''' and select '''New Terminal.'''
+
|| In the menu bar, click on '''terminal''' and select '''New Terminal'''.
+
  
We can see a terminal window at the bottom.
+
<div style="color:#000000;">We can see a terminal window at the bottom.</div>
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| Create direcotry '''MyRustProject'''
+
|| <span style="color:#000000;">Create direcotry </span><span style="color:#000000;">'''MyRustProject'''</span>
  
>mkdir Desktop/MyRustProject
+
<div style="color:#000000;">>mkdir Desktop/MyRustProject</div>
  
>cd Desktop/MyRustProject
+
<div style="color:#000000;">>cd Desktop/MyRustProject</div>
  
|| Let us create a directory '''MyRustProject''' on the '''Desktop'''.
+
|| <span style="color:#000000;">Let us create a directory </span><span style="color:#000000;">'''MyRustProject'''</span><span style="color:#000000;"> </span>on the<span style="color:#000000;"> desktop.</span><span style="color:#000000;">In the terminal</span>, <span style="color:#000000;">type as shown to create a directory </span><span style="color:#000000;">'''MyRustProject'''</span>.
  
In the terminal, type as shown to create a directory '''MyRustProject'''.
+
We will save all the Rust Projects in this directory required for this series.
 +
|- style="border:1pt solid #252525;padding:0cm;"
 +
|| <div style="color:#000000;">Type</div>
  
We will save all the '''Rust''' Projects in this directory required for this series.
+
<span style="color:#000000;">></span><span style="color:#000000;">'''cargo new hellowor</span>ld'''
|-
+
|| <span style="color:#000000;">Type the command </span><span style="color:#000000;">'''cargo new hellowor</span>ld'''
|| Type
+
  
>'''cargo new helloworld'''
+
<div style="color:#000000;">A binary cargo application has been created.</div>
|| Type the command '''cargo new helloworld'''.
+
|- style="border:1pt solid #252525;padding:0cm;"
 +
|| <div style="color:#000000;">Click File>> Open folder</div>
  
A binary '''cargo''' application has been created.
+
<div style="color:#000000;">Select hello folder under MyRustPRoject</div>
|-
+
|| <span style="background-color:#ffffff;color:#000000;">Open the created project by clicking on the </span><span style="background-color:#ffffff;color:#000000;">'''File'''</span><span style="background-color:#ffffff;color:#000000;"> menu in the menu bar and then </span><span style="background-color:#ffffff;color:#000000;">'''Open folder.'''</span>
|| Click File>> Open folder
+
  
Select hello folder under MyRustPRoject
+
<span style="background-color:#ffffff;color:#000000;">Go to </span><span style="background-color:#ffffff;color:#000000;">'''Desktop'''</span><span style="background-color:#ffffff;color:#000000;"> and then select </span><span style="background-color:#ffffff;color:#000000;">'''MyRustProject'''</span><span style="background-color:#ffffff;color:#000000;">.</span>
  
|| Open the created project by clicking on the '''File''' menu in the menu bar and then '''Open Folder.'''
+
<span style="background-color:#ffffff;color:#000000;">Locate the </span><span style="color:#000000;">'''helloworld'''</span><span style="background-color:#ffffff;color:#000000;"> folder.</span>
  
Go to '''Desktop''' and then select '''MyRustProject'''.
+
<span style="background-color:#ffffff;color:#000000;">Then click on the </span><span style="background-color:#ffffff;color:#000000;">'''open'''</span><span style="background-color:#ffffff;color:#000000;"> button at the top right corner.</span>
 +
|- style="border:1pt solid #252525;padding:0cm;"
 +
| style="color:#000000;" | '''Click src folder> main.rs'''
 +
|| <span style="background-color:#ffffff;color:#000000;">In the left panel,under the </span><span style="background-color:#ffffff;color:#000000;">'''EXPLORER'''</span><span style="background-color:#ffffff;color:#000000;"> section, expand the project </span><span style="color:#000000;">'''hello'''</span><span style="background-color:#ffffff;color:#000000;"> by clicking on it.</span>
  
Locate the '''helloworld''' folder.
+
<span style="color:#000000;">Click the </span><span style="color:#000000;">'''src'''</span><span style="color:#000000;"> folder, and you can see that a file called </span><span style="color:#000000;">'''main.rs'''</span><span style="color:#000000;"> is created.</span>
  
Then click on the '''Open''' button at the top right corner.
+
Click on it to open the file.
|-
+
|| '''Click src folder> main.rs'''
+
|| In the left panel, under the '''EXPLORER''' section, expand the project '''HELLO''' by clicking on it.
+
  
Click the '''src''' folder, and you can see that a file called '''main.rs''' is created.
+
<span style="color:#000000;">All the code has to be run using this particular file </span><span style="color:#000000;">'''main.rs.'''</span>
  
Click on it to open the file.
+
<div style="color:#000000;">By default, we can see a hello world program on the right panel.</div>
 +
|- style="border:1pt solid #252525;padding:0cm;"
 +
| style="color:#000000;" |
 +
| style="color:#000000;" | Let us see the Rust program structure.
 +
|- style="border:1pt solid #252525;padding:0cm;"
 +
|| <div style="color:#000000;">'''Highlight according to narration.'''</div>
  
All the code has to be run using this particular file '''main.rs'''.
+
<div style="color:#000000;">'''fn main() {'''</div>
  
By default, we can see a hello world program on the right panel.
+
<div style="color:#000000;">'''println!("Hello, World!");'''</div>
|-
+
||
+
|| Let us see the '''Rust''' program structure.
+
|-
+
|| '''Highlight according to narration.'''
+
  
'''fn main() {'''
+
<div style="color:#000000;">'''}'''</div>
  
'''println!("Hello, World!");'''
+
|| <span style="color:#000000;">Every Rust program </span>starts<span style="color:#000000;"> with this </span><span style="color:#000000;">'''`main`'''</span><span style="color:#000000;"> function.</span>
  
'''}'''
+
<span style="color:#000000;">Inside the curly braces </span><span style="color:#000000;">'''`{ ... }`'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''Println '''</span><span style="color:#000000;">command prints the text hello world.</span>
  
|| Every Rust program starts with this '''`main`''' function.
+
<span style="color:#000000;">The </span><span style="color:#000000;">'''semicolon `;`'''</span><span style="color:#000000;"> at the end indicates the end of the command.</span>
  
Inside the curly braces '''`{ ... }`''' '''Println '''command prints the text hello world.
+
<span style="color:#000000;">Save the file by pressing </span><span style="color:#000000;">'''Ctrl + S.'''</span>
  
The '''semicolon `;`''' at the end indicates the end of the command.
+
<div style="color:#000000;">Let us compile and run the program.</div>
 +
|- style="border:1pt solid #252525;padding:0cm;"
 +
| style="color:#000000;" | Click on terminal> new terminal
 +
|| <span style="color:#000000;">In the menu bar, click on </span><span style="color:#000000;">'''terminal'''</span><span style="color:#000000;"> and select </span><span style="color:#000000;">'''New Terminal.'''</span>
  
Save the file by pressing '''Ctrl + S'''.
+
Make sure that we are inside the cargo project .i.e '''helloworld'''
 +
|- style="border:1pt solid #252525;padding:0cm;"
 +
| style="color:#000000;" | '''> cargo build'''
 +
|| <span style="color:#000000;">In the terminal. type </span><span style="color:#000000;">'''cargo build '''</span><span style="color:#000000;">to compile the Cargo project.</span>
  
Let us compile and run the program.
+
<div style="color:#000000;">When successfully compiled, we see a finished message.</div>
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| Click on terminal> new terminal
+
| style="color:#000000;" | '''>cargo run'''
|| In the menu bar, click on '''terminal''' and select '''New Terminal'''.
+
|| <div style="color:#000000;">As the project is compiled successfully, it’s time to run the project.</div>
  
Make sure that we are inside the cargo project that is, '''helloworld'''
+
<span style="color:#000000;">Then type </span><span style="color:#000000;">'''cargo run'''</span>
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| '''> cargo build'''
+
| style="color:#000000;" | '''Point to the output'''
|| In the terminal. type '''cargo build ''' to compile the '''Cargo''' project.
+
| style="color:#000000;" | Now we see Hello world! printed in the terminal.
 +
|- style="border:1pt solid #252525;padding:0cm;"
 +
|| <div style="color:#000000;">'''Type '''</div>
  
When successfully compiled, we see a finished message.
+
<span style="background-color:#ffffff;">'''Println!</span><span style="background-color:#ffffff;color:#000000;">(“Welcome to Rust Spoken Tutorial”);'''</span>
|-
+
|| '''>cargo run'''
+
|| As the project is compiled successfully, it’s time to run the project.
+
  
Then type '''cargo run'''.
+
<div style="color:#000000;">'''Press Ctrl+s'''</div>
|-
+
|| <div style="color:#000000;">Let us add one more print statement in the program.</div>
|| '''Point to the output'''
+
|| Now we see '''Hello world!''' printed in the terminal.
+
|-
+
|| '''Type '''
+
 
+
'''Pritnln(“Welcome to Rust Spoken Tutorial”);'''
+
'''Press Ctrl+S'''
+
|| Let us add one more print statement in the program.
+
  
 
In the code window, type a print statement “welcome to Rust spoken tutorial” as shown.
 
In the code window, type a print statement “welcome to Rust spoken tutorial” as shown.
  
Press Ctrl + S to save the file.
+
<span style="color:#000000;">Press </span>c<span style="color:#000000;">trl+s to save the file.</span>
  
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| '''> cargo run'''
+
| style="color:#000000;" | '''> cargo run'''
|| In the terminal, type '''cargo run'''
+
|| <div style="color:#000000;"></div>
  
We can see both the text messages as output.
+
<span style="color:#000000;">In the terminal, type</span><span style="color:#252525;"> </span><span style="color:#252525;">'''cargo run'''</span>
|-
+
 
||  
+
<span style="color:#000000;">We can see both the text </span>messages<span style="color:#000000;"> as output.</span>
|| After switching back, I directly did a '''cargo run''' without '''compiling''' the project again.
+
|- style="border:1pt solid #252525;padding:0cm;"
 +
| style="color:#000000;" |  
 +
|| After switching back, I directly did a cargo run without compiling the project again.
  
 
Did you notice that?
 
Did you notice that?
  
When we do a '''cargo run, ''' the '''package manager '''will '''build '''again and '''run '''the '''project'''.
+
When we do a '''cargo run, '''the '''package manager '''will '''build '''again and '''run '''the '''project'''.
  
So going forward, we can directly use '''cargo run ''' to '''build '''and '''run '''the '''project '''in one go.
+
So going forward, we can directly use '''cargo run '''to '''build '''and '''run '''the '''project '''in one go.
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| '''Click on cargo.toml'''
+
| style="color:#000000;" | '''Click on cargo.toml'''
|| In the left panel, open the file '''Cargo.toml.'''
+
|| <span style="background-color:#ffffff;color:#000000;">In the left panel, open the file </span><span style="background-color:#ffffff;color:#000000;">'''Cargo.toml.'''</span>
  
We can see two sections namely '''package''' and '''dependencies'''.
+
<span style="background-color:#ffffff;color:#000000;">We see two sections namely </span><span style="background-color:#ffffff;color:#000000;">'''package'''</span><span style="background-color:#ffffff;color:#000000;"> and </span><span style="background-color:#ffffff;color:#000000;">'''dependencies.'''</span>
  
This is where we can write all the project related information like name, version, authors, edition.
+
<div style="color:#000000;">This is where we can write all the project related information like name, version, authors, edition.</div>
  
We can even mention dependencies which the project depends upon.
+
<div style="color:#000000;">We can even mention dependencies which the project depends upon.</div>
|-
+
|- style="border:1pt solid #252525;padding:0cm;"
|| '''Show Slide: '''
+
|| <div style="color:#000000;">Show Slide:</div>
  
'''Summary'''
+
<div style="color:#000000;">'''Summary'''</div>
|| This brings us to the end of this tutorial.
+
|| <div style="color:#000000;">This brings us to the end of this tutorial.</div>
  
Let us summarize.  
+
<div style="color:#000000;">Let us summarize. </div>
 +
|- style="border:1pt solid #252525;padding:0cm;"
 +
|| Slide:
  
In this tutorial, we have learnt to
+
Assignment
* Create a cargo project
+
* Write a Hello world '''rust''' program
+
* Compile and Run the program
+
 
+
|-
+
||  '''Show Slide: '''
+
 
+
'''Assignment'''
+
  
 
|| As an assignment,
 
|| As an assignment,
* Create a cargo project named''' assignment'''
+
* <div style="margin-left:1.27cm;margin-right:0cm;">Create a cargo project named''' assignment'''</div>
* Print - “ Welcome to Rust Assignment”
+
* <div style="margin-left:1.27cm;margin-right:0cm;">Print - “ Welcome to Rust Assignment”</div>
* Compile and execute the project
+
* <div style="margin-left:1.27cm;margin-right:0cm;">Compile and execute the project</div>
* Observe the output in the Terminal
+
* <div style="margin-left:1.27cm;margin-right:0cm;">Observe the output in the Terminal</div>
 
+
|-  
+
|| '''Show Slide: '''
+
 
+
'''About Spoken Tutorial Project'''
+
|| The video at the following link summarizes the Spoken Tutorial Project.
+
 
+
Please download and watch it.
+
|-  
+
||  '''Show Slide: '''
+
 
+
'''Spoken Tutorial Workshops'''
+
|| The '''Spoken Tutorial Project''' team conducts workshops and gives certificates.
+
 
+
For more details, please write to us.
+
|-
+
||  '''Show Slide: '''
+
 
+
'''Forum for specific questions'''
+
|| Please post your timed queries in this forum.
+
 
+
|-
+
||  '''Show Slide: '''
+
 
+
'''Acknowledgement'''
+
|| The''' Spoken Tutorial''' Project was established by the '''Ministry of Education, Government of India'''.
+
|-
+
||  '''Show Slide: '''
+
 
+
'''Acknowledgement'''
+
|| We would like to thank '''Vishal Pokuri from VIT Vellore '''for the content contribution.
+
|-
+
 
+
||  '''Show Slide: '''
+
  
'''Thank you'''
+
|- style="border:1pt solid #252525;padding-top:0.176cm;padding-bottom:0.176cm;padding-left:0.123cm;padding-right:0.176cm;"
|| This tutorial is contributed by '''Nirmala Venkat''' and '''Ketki Bhamble''' from the spoken tutorial team.
+
|| <div style="color:#000000;">Show Slide: </div>
  
Thank you for joining.
+
<div style="color:#000000;">'''Thank you'''</div>
 +
|| <span style="color:#000000;">Thank you for joining.</span>
 
|-
 
|-
 
|}
 
|}

Revision as of 15:02, 10 July 2025

Visual Cue Narration
Show Slide:
Title Slide
Welcome to the Spoken tutorial on Hello World using Cargo.
Show Slide:
Learning Objectives
In this tutorial, we will learn how to
  • Create a cargo project
  • Write a Hello world rust program
  • Compile and Run the cargo project
Show Slide:
System Requirements
This tutorial is recorded using
  • Ubuntu Linux OS version 22.04
  • Rust version 1.80.0
  • Visual studio code version 1.19.0
Show slide:

Prerequisite

To follow this tutorial,
  • You must have basic knowledge of any programming language
  • You should also have Rust installed on your system.
 Slide:
About Cargo
  • Cargo is a Rust build system and package manager
  • Cargo handles the compilation process and automates the building of your project.
  • Cargo allows you at add and manage external libraries
  • In the Cargo.toml file, you can list your dependencies.
  • Complex Rust programs need these dependencies to develop Rust projects.
Open Visual code editor
Let us open the Visual Studio Code editor.
click on terminal and select New Terminal. In the menu bar, click on terminal and select New Terminal.
We can see a terminal window at the bottom.
Create direcotry MyRustProject
>mkdir Desktop/MyRustProject
>cd Desktop/MyRustProject
Let us create a directory MyRustProject on the desktop.In the terminal, type as shown to create a directory MyRustProject.

We will save all the Rust Projects in this directory required for this series.

Type

>cargo new helloworld

Type the command cargo new helloworld
A binary cargo application has been created.
Click File>> Open folder
Select hello folder under MyRustPRoject
Open the created project by clicking on the File menu in the menu bar and then Open folder.

Go to Desktop and then select MyRustProject.

Locate the helloworld folder.

Then click on the open button at the top right corner.

Click src folder> main.rs In the left panel,under the EXPLORER section, expand the project hello by clicking on it.

Click the src folder, and you can see that a file called main.rs is created.

Click on it to open the file.

All the code has to be run using this particular file main.rs.

By default, we can see a hello world program on the right panel.
Let us see the Rust program structure.
Highlight according to narration.
fn main() {
println!("Hello, World!");
}
Every Rust program starts with this `main` function.

Inside the curly braces `{ ... }` Println command prints the text hello world.

The semicolon `;` at the end indicates the end of the command.

Save the file by pressing Ctrl + S.

Let us compile and run the program.
Click on terminal> new terminal In the menu bar, click on terminal and select New Terminal.

Make sure that we are inside the cargo project .i.e helloworld

> cargo build In the terminal. type cargo build to compile the Cargo project.
When successfully compiled, we see a finished message.
>cargo run
As the project is compiled successfully, it’s time to run the project.

Then type cargo run

Point to the output Now we see Hello world! printed in the terminal.
Type

Println!(“Welcome to Rust Spoken Tutorial”);

Press Ctrl+s
Let us add one more print statement in the program.

In the code window, type a print statement “welcome to Rust spoken tutorial” as shown.

Press ctrl+s to save the file.

> cargo run

In the terminal, type cargo run

We can see both the text messages as output.

After switching back, I directly did a cargo run without compiling the project again.

Did you notice that?

When we do a cargo run, the package manager will build again and run the project.

So going forward, we can directly use cargo run to build and run the project in one go.

Click on cargo.toml In the left panel, open the file Cargo.toml.

We see two sections namely package and dependencies.

This is where we can write all the project related information like name, version, authors, edition.
We can even mention dependencies which the project depends upon.
Show Slide:
Summary
This brings us to the end of this tutorial.
Let us summarize.
Slide:

Assignment

As an assignment,
  • Create a cargo project named assignment
  • Print - “ Welcome to Rust Assignment”
  • Compile and execute the project
  • Observe the output in the Terminal
Show Slide:
Thank you
Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat