2024-06-21

This commit is contained in:
2024-06-21 14:47:31 +09:00
parent b9439ad55b
commit 37a3c0d4f8
17 changed files with 248 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
# Rust
## rustup
먼저, 러스트 툴체인 인스톨러를 설치합니다.
```bash
sudo snap install rustup --classic
```
```bash
rustup -V # 버전 확인
```
RustUp을 통해서 Rust를 설치합니다.
```bash
rustup install stable
rustup default stable
```
또는
```bash
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
```
## 프로젝트 생성
```bash
mkdir my_project
cd my_project
cargo init
```
또는,
```bash
cargo new my_project
```
위 명령을 실행하면 프로젝트 폴더에 'Cargo.toml' 과 'src/main.rs' 등의 파일이 생성됩니다.
### Hello, world!
위에서 생성된 main.rs 파일은 간단한 헬로 월드 프로그램입니다.
```rust
fn main() {
println!("Hello, world!");
}
```
`println`은 화면에 한 줄 출력하는 매크로입니다. 러스트에서는 매크로 이름 뒤에 `!`가 붙습니다.
```bash
cargo build
```
`./target/debug` 디렉토리에 실행 파일이 만들어집니다. 다음 명령으로 프로그램을 실행시키면 화면에 'Hello, world!'가 출력됩니다.
```bash
./target/debug/my_project
```
배포용으로 컴파일하려면, release 옵션을 추가합니다.
```bash
cargo build --release
```
`cargo run` 명령을 사용하면 빌드와 동시에 실행할 수 있습니다.
```bash
cargo run
```
또는,
```bash
cargo run --release
```
## rustfmt
소스 코드를 깔끔하게 정리해 줍니다.
```bash
rustfmt ./src/main.rs
```
또는, 아래 명령으로 전체 소스 파일에 대해 처리할 수 있습니다.
```bash
cargo fmt
```