let a = 10
let a = 20 (o)
a = 30(x)
const MAX_POINTS: u32 = 100_000;
fn main() { let x = 5; let x = x + 1; let x = x * 2; println!("The value of x is: {}", x); }
fn main() { let spaces = " "; let spaces = spaces.len(); }
--> 변수유형이 바뀜으로 컴파일 에러
러스트는 타입이 고정된 언어입니다.
정수형
- isize와 usize는 컴퓨터 아키텍쳐를 따릅니다.
- i32가 제일 연산 속도가 빠릅니다.
부동 소수점
fn main() {
let x = 2.0; // f64
let y: f32 = 3.0; // f32
}
- IEEE-754 표준을 따릅니다.
fn main() {
// addition
let sum = 5 + 10;
// subtraction
let difference = 95.5 - 4.3;
// multiplication
let product = 4 * 30;
// division
let quotient = 56.7 / 32.2;
// remainder
let remainder = 43 % 5;
}
fn main() {
let t = true;
let f: bool = false; // with explicit type annotation
}
fn main() {
let c = 'z';
let z = 'ℤ';
let heart_eyed_cat = '😻';
}
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}
fn main() {
let a = [1, 2, 3, 4, 5];
}
fn main() {
another_function(5);
}
fn another_function(x: i32) {
println!("The value of x is: {}", x);
}
fn five() -> i32 {
5
}
fn main() {
let x = five();
println!("The value of x is: {}", x);
}
fn main() {
let number = 6;
if number % 4 == 0 {
println!("number is divisible by 4");
} else if number % 3 == 0 {
println!("number is divisible by 3");
} else if number % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
}
fn main() {
let condition = true;
let number = if condition {
5
} else {
6
};
println!("The value of number is: {}", number);
}
fn main() {
loop {
println!("again!");
}
}
fn main() {
let mut number = 3;
while number != 0 {
println!("{}!", number);
number = number - 1;
}
println!("LIFTOFF!!!");
}
fn main() {
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}
-> 4에서 1로 큰수에서 작은수로 내려가며 반복한다.