From setting up a Rust environment to running Hello World

Setting up a Rust environment

Here are the steps to set up your Rust environment. For more information on Rust, see here (https://www.rust-lang.org).

Execution environment

The PC itself is a Mac, but I'm running it on Docker because it was troublesome to reinstall.

Execution environment


Docker version 19.03.5, build 633a0ea
image: ubuntu 19.04

Install rustup

Follow the Official Installation Method to install. \
You can go to the official website and have it installed while reading the explanation, but if you are troublesome curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh You can install it by running on the terminal. At the time of installation, it will be as follows.

Installation


0a0cc89c09eb# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to
Cargo's bin directory, located at:

  /root/.cargo/bin

This can be modified with the CARGO_HOME environment variable.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /root/.rustup

This can be modified with the RUSTUP_HOME environment variable.

This path will then be added to your PATH environment variable by
modifying the profile file located at:

  /root/.profile

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1  #<-It doesn't have to be 1 here, but it's safer to set it to 1.

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2020-03-12, rust version 1.42.0 (b8cedc004 2020-03-09)
info: downloading component 'cargo'
  4.8 MiB /   4.8 MiB (100 %)   1.3 MiB/s in  3s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 12.0 MiB /  12.0 MiB (100 %)   3.0 MiB/s in  4s ETA:  0s
info: downloading component 'rust-std'
 17.1 MiB /  17.1 MiB (100 %)   3.0 MiB/s in  5s ETA:  0s
info: downloading component 'rustc'
 58.6 MiB /  58.6 MiB (100 %)   5.0 MiB/s in 13s ETA:  0s^[[B
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 12.0 MiB /  12.0 MiB (100 %)   4.3 MiB/s in  2s ETA:  0s
info: installing component 'rust-std'
 17.1 MiB /  17.1 MiB (100 %)  10.6 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 58.6 MiB /  58.6 MiB (100 %)   8.4 MiB/s in  7s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable'

  stable installed - rustc 1.42.0 (b8cedc004 2020-03-09)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done
automatically.

To configure your current shell run source $HOME/.cargo/env

If you want to pass the path with the login shell, as you can see at the end source $HOME/.cargo/env Please run the. If you log in again, the pass will pass, so it is not required.

For Windows environment

The above installation method is the installation method on Linux / Unix. Click here to start the installer and you can install without any problem. I think. Recently, it seems that linux commands can be used on windows with WSL, so it may be possible to execute it on WSL. If you have any questions, please feel free to comment or email.

Hello world in Rust!

Now that the execution environment is ready, let's actually write the program.

hello_world.rs


fn main(){
	println!("hello, world");
}

The editor can be anything, so save the above code and save it with a name. \
At this time, you will need the save destination, so make a note or copy it. After saving, open the terminal and try the matter.

Execution result 1


0a0cc89c09eb# ls
hello_world.rs
0a0cc89c09eb# rustc hello_world.rs
0a0cc89c09eb# ls
hello_world  hello_world.rs
0a0cc89c09eb# ./hello_world
hello, world

In this example, I used rustc to compile, but the following method is the major method.

Execution result 2


0a0cc89c09eb# cargo new --bin hello_world
     Created binary (application) `hello_world` package
0a0cc89c09eb# cd hello_world && tree .
.
|-- Cargo.toml
`-- src
    `-- main.rs

1 directory, 2 files
0a0cc89c09eb# cargo run
   Compiling hello_world v0.1.0 (/root/test/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.34s
     Running `target/debug/hello_world`
Hello, world!

In this case, main.rs already exists and the contents are

rust:./src/main.rs


fn main() {
    println!("Hello, world!");
}

From the beginning, hello world is written like this. Most of the code doesn't need to use Cargo while studying for the first time, but in the end you will use Cargo, so you may want to get used to it from the beginning.

reference

https://forge.rust-lang.org/infra/other-installation-methods.html https://rust-lang.org/learn/get-started

Recommended Posts

From setting up a Rust environment to running Hello World
From building a Python environment for inexperienced people to Hello world
From Kivy environment construction to displaying Hello World
From setting up Raspberry Pi to installing Python environment
From nothing on Ubuntu 18.04 to setting up a Deep Learning environment in Tensor
Everything from building a Python environment to running it on Windows
Flask tutorial (from installation to hello world)
To myself as a Django beginner (3)-Hello world! ---
RaspberryPi3 (STRETCH) setup from OS installation to Hello World
From buying a computer to running a program with python
Setting up Jupyter Lab in a Python 3.9 venv environment
Summary from building Python 3.4. * From source to building a scientific computing environment
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➀
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➁
RabbitMQ Tutorial (1) -From Installation to Hello World (Official Site Translation)
I came up with a way to create a 3D model from a photo Part 01 Creating an environment
From installing Ansible to building a Python environment in Vagrant's virtual environment
Introduction to TensorFlow --Hello World Edition
From Ubuntu 20.04 introduction to environment construction
Introduction to Ansible Part 1'Hello World !!'
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
When running a Python shell from Electron, pass multiple arguments to run Python.
I came up with a way to make a 3D model from a photo.
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
C language to see and remember Part 1 Call C language from Python (hello world)
Re: Heroku life begin with Flask from zero - Environment and Hello world -
Try to set up a Vim test environment quite seriously (for Python)
[Python] Web application from 0! Hands-on (2) -Hello World-
How to create a clone from Github
How to build a sphinx translation environment
Execute a script from Jupyter to process
Send a message from Python to Slack
I want to build a Python environment
How to create a repository from media
How to display Hello world in python
Add a Python virtual environment to VSCode
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➀
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➁
Let's try TensorFlow music generation project "Magenta" from development environment setting to song generation.
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack