Rust 기초

Rust Data Types: Integers, Floats, bool and char

Pick the right type for each value and convert between numbers with as.

  • 초급
  • 2분 분량
  • 실행 가능한 예제 1개
배울 내용
  • u8, i32, i64 and f64
  • bool and char
  • Converting with as
Value → type
30u8, a small whole number
-4.5f64, a decimal number
truebool
'A'char

Every value has a type known at compile time

Rust is statically typed: every value has a type that the compiler knows. Integers come in sizes such as u8 and i64, f64 holds decimal numbers, bool is true or false, and char is one Unicode character.

Rust never converts numbers silently, so age as f64 is needed before adding a float. Underscores in 1_000_000 only make the number easier to read. u8::MAX is the largest value a u8 can hold.

핵심 포인트

  1. Integer types say their size: u8 holds 0 to 255.
  2. Rust never converts numbers silently: write age as f64.
  3. char is one Unicode character, written in single quotes.

실행 가능한 Rust 예제

Rust
코드 실행 →
main.rs
fn main() {
    let age: u8 = 30;
    let temperature: f64 = -4.5;
    let is_ready: bool = true;
    let grade: char = 'A';
    let big = 1_000_000i64;

    let total = age as f64 + temperature;
    println!("{age} {temperature} {is_ready} {grade} {big}");
    println!("total = {total}");
    println!("u8 max = {}", u8::MAX);
}
이 예제의 실행 버튼 만들기 코드 실행 결과
30 -4.5 true A 1000000
total = 25.5
u8 max = 255

연습

예제를 온라인 컴파일러에서 열고 값을 바꾸거나 한 줄을 추가한 다음, 실행하기 전에 출력을 예상해 보세요. 그런 다음 일부러 코드를 망가뜨리고 컴파일러 메시지를 읽어 보세요.

이 레슨을 마쳤나요?

학습 완료로 표시하면 진행 상황을 기록할 수 있습니다.

다음: Operators and Arithmetic →

챌린지로 연습하기

이 레슨에서 배운 내용으로 실제 문제를 풀어 보세요. 모든 챌린지는 브라우저에서 실행되며 즉시 테스트와 AI 리뷰를 받을 수 있습니다.

모든 챌린지