728x90

Getting Started

Install Rust for windows

1) Visual Studio 2022 설치 
   [include]

  • Desktop Development with C++ 
  • The Windows 10 or 11 SDK
  • The English language pack component, along with any other language pack of your choosing

2) Rust 설치
3) 설치결과 확인

$ rustc --version
rustc 1.71.1 (eb26296b5 2023-08-03)

# Check C:\Users\*****\.cargo\bin
$ ehco %path%

Create New Project 

#create 'helloworld' project 
$ cargo new helloworld --bin
$ cd helloworld

Hello World

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

Compiling and Running 

# Compiling 
$ rustc .\src\main.rs

# Running 
$ main.exe
Hello world!

Cargo : Building and Running 

# building
$ cargo build 
   Compiling helloworld v0.1.0 (c:/projects/helloworld)
    Finished dev [unoptimized + debuginfo] target(s) in 2.85 secs

# run
$ cargo run 
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/helloworld`
Hello, world!

# check code errors
$ cargo check

# building for release (executable in target/release.)
cargo build --release

Cargo Commands

더보기
  • We can create a project using cargo new.
  • We can build a project using cargo build.
  • We can build and run a project in one step using cargo run.
  • We can build a project without producing a binary to check for errors using cargo check.
  • Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.

Variables and Mutabililty

- 기본 변수는 불변성(immutable : 재할당 불가) 가짐.

- 가변성(mutable : 재할당 가능)을 가진 변수는 let mut 로 선언.

- 상수는 const 키워드 사용. 상수 선언시에는 타입을 지정.

- 선언한 변수를 사용하지 않으면 컴파일러가 오류를 발생시킴.

fn main() {
    let a = 5; 	// immutable (다른 값으로 재할당 안됨)
    //a = 6; // riase error
    println!("a={a}");
    
    let mut b = 100; // mutable
    println!("b={b}");
    b = 101;
    println!("b={b}");
    
    const CONST_VAR:i32 = 200; // int32 type constant
    println!("CONST_VAR={CONST_VAR}");
}

 

...ing...

 

Reference : 

 

RUST Cheat Sheet for Beginner

 

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기
반응형