Rust

Rust 데이터 타입

hellowhales 2024. 7. 14. 15:40
728x90

https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html

 

Variables and Mutability - The Rust Programming Language

As mentioned in the “Storing Values with Variables” section, by default, variables are immutable. This is one of many nudges Rust gives you to write your code in a way that takes advantage of the safety and easy concurrency that Rust offers. However, y

doc.rust-lang.org

 

Rust는 모든 데이터 타입을 정교하게 컨트롤 한다.

Length Signed Unsigned float char
8-bit i8 u8    
16-bit i16 u16    
32-bit i32 u32 f32 char
64-bit i64 u64 f64  
128-bit i128 u128    
arch isize usize    

*arch : 컴퓨터 아키텍처에 따라 정해지는 값

 

데이터 타입 지정 방식

fn main() {
    let x: i32 = 2
}

 

복합형 데이터 타입

-튜플

fn main() {
    let mut tup: (i32, f64, char) = ( 500, 6.4, 's');
    println!("{}", tup.0);
    tup.2 = 'k';
    println!("{}", tup.2);
}

 

-배열

fn main() {
    let mut a: [i32; 5] = [1,2,3,4,5];
    a[0] = 6;
    println!("{}", a[0]);
}

 

 

당연히 모든 데이터 타입은 기본적으로 immutable 하므로 선언 후 변경하려면 mut 선언을 해줘야한다. 

728x90