728x90
✅ 1. Rust 설치
마지막 시간은 Rust 프로젝트를 생성하고 어떠한 방식으로 실행하는지를 되새기면서 이번 공략을 마치겠습니다. Rust 설치되어야 프로젝트를 생성할 수 있습니다.
# rustup 설치
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 설치 확인
rustc --version
cargo --version
✅ 2. Rust 프로젝트 생성
Rust는 Cargo라는 빌드 도구를 기본으로 제공합니다. 다음 명령어로 Rust 프로젝트를 생성할 수 있습니다.
cargo new myproject
- myproject: 프로젝트 이름
- 생성 후 디렉터리 구조
my_project
├── Cargo.toml
└─ src/
└─ main.rs
- 기본 출력
Hello, world!
✅ 3. Rust 기본 출력 예제
fn main() {
// 변수 선언
let name = "Rust";
let age = 6;
// 출력
println!("Hello, {}! Rust is {} years old", name, age);
// 조건문
if age > 5 {
println!("Rust is still young!")
}
// 반복문
for i in 1..4 {
println!("Iteration {}", i)
}
// 함수 호출
let sum = add(3,5);
println!("3 + 5 = {}", sum);
}
fn add(a: i32, b: i32) -> i32 {
a + b
}
- 출력 예시
Hello, Rust! Rust is 6 years old
Rust is still young!
Iteration 1
Iteration 2
Iteration 3
3 + 5 = 8
✅ 4. Rust 기본 예제 코드
- 백터(Vector) 사용
fn main() {
let mut nums = vec![1,2,3];
nums.push(4);
for n in &nums {
println!("{}", n)
}
}
- 문자열 처리
fn main() {
let mut s = String::from("Hello");
s.push_str(", Rust!");
println!("{}", s);
}
- 구조체(Struct)
struct Person {
name: String,
age: u8
}
fn main() {
let p = Person {
name: String::from("Alice"),
age: 30,
};
println!("{} is {} years old", p.name, p.age)
}
- 열거형(Enums)
#[allow(dead_code)]
enum Direction {
Up,
Down,
Left,
Right
}
fn main() {
let dir = Direction::Up;
match dir {
Direction::Up => println!("Going Up!"),
Direction::Down => println!("Going Down"),
_ => println!("Other Direction")
}
}
✅ 5. Tip
- cargo build -> 빌드만
- cargo run -> 빌드 후 실행
- cargo check -> 문법만 검사, 빠르게 확인 가능함
✅ 6. Rust 추가 사항
- 📗 공식 문서
- 한국어 공식 문서: https://doc.rust-kr.org/
- 🔖 주요 콘텐츠
- The Rust Programming Language: Rust 핵심 개념과 문법을 다루는 공식 교제
- Rust by Example: 실용적인 예제와 Rust를 학습할 수 있는 자료
- Rust by Reference: Rust 언어의 세부적인 문법과 규칙을 설명
- Rustonomicon: Rust의 고급 기능과 내부 동작을 깊이 있게 다루는 문서
- 💊 학습자료 및 연습 문제
- Rustlings: 짧은 연습 문제를 통해 Rust의 기본 개념을 학습할 수 있는 자료
- Rust 공식 튜토리얼: Rust의 기본부터 고급주제까지 다루는 공식 튜토리얼
- ⏱️학습 팁
- 소유권 모델 이해: Rust의 소유권, 빌림, 라이프타임 개념은 초보자에게 어려울 수 있는 개념이므로 간단한 예제부터 시작해 이해하는 것을 추천
- 빌림 검사기 활용: 컴파일러의 오류 메시지를 통해 Rust의 메모리 안정성을 이해하고, 이를 활용하여 안전한 코드를 작성하는 연습이 필요
- 간단한 프로젝트 만들기: 간단한 애플리케이션을 개발해 Rust의 기능과 라이브러리를 실습
GitHub - Koras02/rust-tutorial: https://thinky.tistory.com/category/Back-End/Rust
https://thinky.tistory.com/category/Back-End/Rust. Contribute to Koras02/rust-tutorial development by creating an account on GitHub.
github.com
728x90
LIST
'Back-End > Rust' 카테고리의 다른 글
[Rust] 14장 매크로 (4) | 2025.08.13 |
---|---|
[Rust] 13장 메모리 (2) | 2025.08.09 |
[Rust] 12장 비동기 프로그래밍 (1) | 2025.08.05 |
[Rust] 11장 고차 함수 (0) | 2025.04.04 |
[Rust] 10장 트레이트 (0) | 2025.03.25 |