Rust/C2/Printing-Hello-World-using-Cargo/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of the script: Printing Hello, World using Cargo

Author: Jayesh Katta Ramalingaiah

Domain Reviewer: Vigneshwer Dhinakaran

Novice Reviewer: Praveen S.

Keywords: Rust, cargo, package manager, function, rs


Visual Cue
Narration
Slide: Title Welcome to the spoken tutorial on Printing Hello World using Cargo.
Slide:

Learning Objectives

In this tutorial we will learn:
  • How to create a Cargo project and
  • Execute it.
Slide: System Specifications This tutorial is recorded using:
  • Ubuntu Linux OS version 18.04
  • Rust version 1.47.0
  • Visual Studio Code version 1.45.0 (code editor)

However you may use any other editor of your choice.

Slide : Pre-requisites
  • To practice this tutorial, you should be familiar with basic Linux commands.
  • If not, please go through the prerequisite tutorials on this website.
Slide: Why Cargo?
  • Cargo is a Rust build system and package manager
  • Rustaceans use cargo to manage their projects as it makes the developer’s life easier.
Slide: What does Cargo do?
  • Cargo takes care of building our code
  • Downloading and managing libraries that our code depends on.
  • Complex Rust programs need these dependencies to develop Rust projects.
Slide: Cargo Commands Let’s begin understanding the commands first.


  • 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.
Press Ctrl+Alt+T keys Open the terminal by pressing Ctrl, Alt and T keys simultaneously on the keyboard.


Ensure that you have root permissions to run the commands.

Only Narration Here onwards, please remember to press the Enter key after typing each command.
[Terminal] Type:


cd Desktop/MyRustProject

[Enter]

Using cd command go to the Rust practice folder which we created earlier.
[Terminal] Type:


cargo new sample_hello_world [Enter]

Now let us create a Cargo project named sample_hello_word


To do so, type the command as shown.

[Terminal] Highlight:


Created binary (application) `sample_hello_world` package

A binary cargo application has been created.


Open Visual Studio Code editor. You may use any editor of your choice.


I will use Visual Studio Code editor for this demonstration.

[Editor]

Welcome Page ->

Open Folder ->

sample_hello_world

Open the created project by clicking on the Open folder link in the Welcome page.


Browse and locate the folder “sample_hello_world”.


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

[Editor]

Click on SAMPLE_HELLO_WORLD

On the EXPLORER section, expand the project folder “SAMPLE_HELLO_WORLD” by clicking on it.
[Editor]

For respective names - show cursor in the left side pane

We see the src folder and a Cargo.toml file created inside the project folder.
[Editor] Click on src


Point to main.rs

Click on the src folder to expand it.


We find main.rs file inside the src folder.

[Editor] Click on main.rs Open the file main.rs by clicking on it.
[Editor - main.rs]

Highlight the code

We can see the hello world program written inside.


By default, when you create a cargo project, it creates the main.rs file inside the src folder.


And a main function would be written by default.

[Editor] Click on Cargo.toml Open the file Cargo.toml from the project folder by clicking on it.
[Editor - Cargo.toml]

Highlight the code

We see two sections namely package and dependencies.


[Editor - Cargo.toml]

Highlight the package section

This is where we can write all the project related information like name, version, authors, edition.
[Editor - Cargo.toml]

Highlight the dependencies section

We can even mention dependencies which the project depends upon.
Switch to terminal Switch back to the terminal.
[Terminal] Type:


cd sample_hello_world

[Enter]

So the Cargo project is created now.


Navigate inside the cargo project sample_hello_world using the cd command.

[Terminal] Type:


cargo build [Enter]

Now type the command to build the Cargo project.
[Terminal] Highlight:


Finished

When successfully compiled, we see a finished message.
Only narration If we encounter any error, it means we have not installed Cargo properly.


Please revisit the installation tutorial and install Cargo properly before continuing.

[Terminal] Type:


cargo run [Enter]

As the project is compiled successfully, it’s time to run the project.


To run, type cargo run.

[Terminal] Highlight:


Hello, world!

Now we see Hello world! printed in the terminal.
Only narration With this we have come to the end of this tutorial.


Let’s summarize.

Slide: Summary In this tutorial, we have learnt how to create, compile and execute a Cargo project.
Slide: Assignment As an assignment,
  • Create a cargo project named rust-assignment
  • Print - ‘Welcome to Spoken Tutorials!’
  • Compile and execute the project.
  • Observe the output in the Terminal
Slide: About Spoken Tutorial Project
  • The video at the following link summarises the Spoken Tutorial project.
  • Please download and watch it.
Slide: Spoken tutorial workshops
  • We conduct workshops using spoken tutorials and give certificates.
  • For more details, please write to us.
Slide: Forum questions Pls post your timed questions in this forum
Slide: Acknowledgement Spoken Tutorial Project is funded by Ministry of Education, Government of India
Slide: Thanks This is Jayesh signing off. Thank you for joining

Contributors and Content Editors

Kr.jayesh, Nancyvarkey