본문 바로가기
언어/Rust

Rust 언어 및 설치

by khd0801 2023. 11. 29.
반응형

 1.  Rust 언어란?

러스트(Rust)는 시스템 프로그래밍을 위한 언어이다. 시스템 프로그램이란 운영체제, 장치 드라이버, 파일시스템, 메모리 관리, 네트워킹 등 제한된 자원을 다루는 프로그래밍이다

 

Rust는 다른 시스템 프로그래 언어에 비해 아래와 같이 여러 장점을 가지고 있다.. 

 

1. 안전성

시스템 프로그래밍을 위한 언어로는 C언어도 있다. 다만 Rust는 C언어에 비해 안전성과  사용성이 편리해졌다. Rust는 컴파일러에서 프로그램 코드의 undefined behavior(미정의 동작) 오류를 잡아 낸다.

예를 들어 C언어에서는 아래 예제의 코드가 이상 없이 컴파일되어진다.

void main(void) {
	unsigned char a[2];
	a[3] = 0xff;
}

 

해당 코드를 실행하면 stack smashing detected 메시지와 Aborted (core dumped) 메세지를 출력하고 죽어버린다. 배열 a는 요소가 a[0], a[1] 뿐인데, a[3]라고 쓰는 것은 C 프로그램밍 언어 표준에 따르면 undefined behavior(미정의 동작)이 된다. Rust에서는 컴파일러에서 여러 검사를 통하여 미정의 동작을 없에주었다. 또 한 대상을 잃은 포인터, 중복 해제, 널 포인터 역참조를 전부 컴파일 시점에 잡고, 배열 참조를 컴파일 시점 검사와 실행 시점 검사로 보호하기 때문에 버퍼 오버런(buffer overrun)이 없다. Rust로 작성한 프로그램은 앞서 본 C 프로그램과 같은 결함을 자겼더라도 오류 메세지와 함꼐 안전하게 종료된다.

 

2. 병렬 프로그래밍

C와 C++은 동시성(concurrency)를 제대로 이용하기 어렵다. 러스트는 뮤텍스(mutex), 조건 변수, 채널, 원자성 지원 등 기존의 동시성 도구를 전부 사용 할 수 있고, 이들이 제대로 쓰이고 있는지도 점검해 준다. 

또한 복잡한 작업 부하를 프로세서 풀에 골고루 분배할 수 있게 도와주며, Read-Copy-Update와 같은 무잠금 동기화 기법을 쓸 수 있게 해주는 등, 일반적인 동시성 기본 요소의 역할을 뛰어넘는 다양한 라이브러리를 제공한다.

 

3. 빠른 속도

시스템 프로그래밍은 HW를 한계까지 쓰는 일과 관계가 많다. 이러한 프로그래밍에서는 사용자 프로그램에게 최대한 많은 자원을 할당해 주기 위해 커널(시스템 프로그래밍)이 자원을 소비해서는 안된다. 

러스트는 효율적인 기본값을 가지고 설계된 언어로서, 메모리의 사용 방식과 프로세서의 자원을 할애하는 방식을 시스템 프로그래머가 제어할 수 있도록 해준다. 

 

4. 협업이 쉬운 개발 환경

러스트는 빌드 도구인 카고(Cargo)를 이용하면 러스트의 공개 패키지 저장소인 crates.io 웹사이트에 올라온 다양한 라이브러리를 손쉽게 사용할 수 있다. 원하는 라이브러리의 이름과 버전 번호를 파일에 적어 두면, 카고가 해당 라이브러리와 라이브러리와 관련된 부속 라이브러리를 차례로 받아서 전부 링크해 준다. 

러스트 라이브러리의 종류는 HTTP 클라이언트와 서버, 모던 그래픽 API, 규격화된 직렬화에 이르기까지 없는게 없다.

 

또한 러스트 언어 자체도 협을을 지원하도록 설계되었다. 러스트의 트레이트(trait)와 제네릭(generic)을 사용하면 다양한 맥락에서 쓸 수 있는 유연한 인터페이스를 가진 라이브러리를 만들 수 있다. 

 

 2. Rust 설치

Rust는 rustup으로 설치하는게 좋다. https://rustup.rs/ 에서 나와있는 설명데로 설치하면 되며, 아래 명령어로 설치 가능하다.

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

 

 

rustup을 선호하는 이유는 러스트의 새 버전이 나왔을 경우 아무것도 클릭할 필요 없이 그냥 rust update라는 명령어 입력만으로 업그레이드가 가능하기 때문이다. 

 

만약 위 명령어 사용시 아래와 같이 에러가 발생된다면 snap으로 설치된 curl를 지우면 apt로 재 설치 하여 rustup을 설치 하길 바란다.

에러 메세지

curl: (23) Failure writing output to destination
rustup: command failed: downloader https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init /tmp/tmp.Y905e9Yplc/rustup-init x86_64-unknown-linux-gnu

 

curl 재설치 명령어

$ sudo snap remove curl

$ sudo apt-get install curl

 

apt-get install curl 명령어 입력시 에러가 발생되면 sudo apt update 명령어 사용 후 다시 설치 명령어를 입력하길 바란다.

 

아래 화면과 같은 로그가 출력되면 rustup은 설치가 완료된것으로 볼 수 있다.

 

$ khd0801@ubuntu20:~/study/rust/install$ 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.

 

Rustup metadata and toolchains will be installed into the Rustup

home directory, located at:


  /home/khd0801/.rustup

 

This can be modified with the RUSTUP_HOME environment variable.

 

The Cargo home directory is located at:

 

  /home/khd0801/.cargo

 

This can be modified with the CARGO_HOME environment variable.

 

The cargo, rustc, rustup and other commands will be added to

Cargo's bin directory, located at:

 

  /home/khd0801/.cargo/bin

 

This path will then be added to your PATH environment variable by

modifying the profile files located at:

 

  /home/khd0801/.profile

  /home/khd0801/.bashrc

 

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 (default)

               profile: default

  modify PATH variable: yes


1) Proceed with installation (default)

2) Customize installation

3) Cancel installation
>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 2023-12-28, rust version 1.75.0 (82e1608df 2023-12-21)

info: downloading component 'cargo'

info: downloading component 'clippy'

info: downloading component 'rust-docs'

 14.3 MiB /  14.3 MiB (100 %)  11.3 MiB/s in  1s ETA:  0s

info: downloading component 'rust-std'

 23.6 MiB /  23.6 MiB (100 %)  11.3 MiB/s in  2s ETA:  0s

info: downloading component 'rustc'

 61.4 MiB /  61.4 MiB (100 %)  11.2 MiB/s in  5s ETA:  0s

info: downloading component 'rustfmt'

info: installing component 'cargo'

  7.5 MiB /   7.5 MiB (100 %)   6.8 MiB/s in  1s ETA:  0s

info: installing component 'clippy'

info: installing component 'rust-docs'

 14.3 MiB /  14.3 MiB (100 %)   1.6 MiB/s in  9s ETA:  0s

info: installing component 'rust-std'

 23.6 MiB /  23.6 MiB (100 %)   5.7 MiB/s in  4s ETA:  0s

info: installing component 'rustc'

 61.4 MiB /  61.4 MiB (100 %)   5.5 MiB/s in 12s ETA:  0s

info: installing component 'rustfmt'

info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

 

  stable-x86_64-unknown-linux-gnu installed - rustc 1.75.0 (82e1608df 2023-12-21)

 

 

Rust is installed now. Great!

 

To get started you may need to restart your current shell.

This would reload your PATH environment variable to include

Cargo's bin directory ($HOME/.cargo/bin).

 

To configure your current shell, run:

source "$HOME/.cargo/env"

 

rustup 설치 환경을 적용하기 위해서는 rustup을 설치한 쉘을 닫고 다시 열거나, source "$HOME/.cargo/env"  명령어를 실행하여 rustup 환경을 현재 쉘에 적용시켜 준다. 그리고 난 후에는 아래 세 가지 명령어를 사용할 수 있게 된다.

$ khd0801@ubuntu20:~/study/rust/install$ cargo --version

cargo 1.75.0 (1d8b05cdd 2023-11-20)

$ khd0801@ubuntu20:~/study/rust/install$ rustc --version

rustc 1.75.0 (82e1608df 2023-12-21)

$ khd0801@ubuntu20:~/study/rust/install$ rustdoc --version

rustdoc 1.75.0 (82e1608df 2023-12-21)

 

 

  • cargo : 러스트의 컴파일 및 패키지 관리자이다. 다양한 쓰임새를 가진 도구이며 새 프로젝트를 시작하고,
               프로그램을 빌드해 시행, 코드가 의존하는 외부 라이브러리를 관리하기 위한 용도로 사용되어 진다.

  • rustc : rust 컴파일러이다. 보통은 cargo를 통해 실행하지만 가끔 직접 실행해야 될 때도 있다.

  • rustdoc : 러스트 문서화 도구이다. 프로그램의 소스 코드에 적절한 형식의 주석으로 문서를 작성해 두면
                  이를 가지고 정리된 HTML 문서를 만들어 준다. rustc 처럼 보통 cargo를 통해 실행한다.

 

지금까지 Rust언어에 대한 장점 설명과 설치 방법, 설치 도구에 대하여 간단히 소개하였다.

다음장에서는 Rust를 간단히 컴파일하고 실행하는 방법을 소개할 것이며, 차근차근 문법에 대하여 설명하도록 하겠다. 

반응형

'언어 > Rust' 카테고리의 다른 글

Rust 패키지 생성 및 컴파일  (0) 2023.11.30

댓글