문제

변수 x
에 대한 타입 명시가 없어 발생한 에러로 보인다.
풀이
소스 코드를 살펴보자.
fn main() {
// TODO: Change the line below to fix the compiler error.
let x;
if x == 10 {
println!("x is ten!");
} else {
println!("x is not ten!");
}
}
주석으로도 변수 선언 부분을 고치라고 나와있는 것을 볼 수 있다.
아래 if 문에서 x의 값이 정수 10
인지 검사하고 있기 때문에, x
에도 정수를 넣어주면 문제가 해결된다.
Rust에서는 변수 선언 시 초기화가 필수이므로 10
을 할당시켜 주었다.
타입 명시는 변수에 값을 할당할 때 기본적으로 되므로 생략하였다.
수정한 코드는 다음과 같다.
fn main() {
// TODO: Change the line below to fix the compiler error.
let x = 10;
if x == 10 {
println!("x is ten!");
} else {
println!("x is not ten!");
}
}
위 코드를 저장하면...

문제 해결!
'Programming Language > RUST' 카테고리의 다른 글
[Restling] exercises/01_variables/variables4.rs 풀기 (0) | 2024.10.11 |
---|---|
[Rustling] exercises/01_variables/variables3.rs 풀기 (0) | 2024.10.11 |
[Rustling] exercises/01_variables/variables1.rs 풀기 (1) | 2024.10.11 |
Rustling 시작하기 (3) | 2024.10.11 |
RUST의 데이터 타입 (0) | 2024.10.10 |