[Rustling] exercises/01_variables/variables5.rs 풀기

2024. 10. 11. 16:47·Programming Language/RUST

문제

에러를 확인해 보니 데이터 타입의 불일치가 문제인 것 같다.

 

풀이

소스 코드를 확인해보자.

fn main() {
    let number = "T-H-R-E-E"; // Don't change this line
    println!("Spell a number: {}", number);

    // TODO: Fix the compiler error by changing the line below without renaming the variable.
    number = 3;
    println!("Number plus two is: {}", number + 2);
}

TODO에서 변수명을 변경하지 말라고 지시하고 있다.

 

맨 아래의 `println!`을 살펴보니 수학 연산을 하기 때문에 `number` 변수를 정수형으로 바꿔주어야 한다.

또한, 불변 변수이므로 값을 재할당 하는 것도 불가능하다.

 

따라서, shadowing을 통해 문제를 해결할 수 있다.

fn main() {
    let number = "T-H-R-E-E"; // Don't change this line
    println!("Spell a number: {}", number);

    // TODO: Fix the compiler error by changing the line below without renaming the variable.
    let number = 3;
    println!("Number plus two is: {}", number + 2);
}

 

저장하면,

해결~

 

저작자표시 비영리 (새창열림)

'Programming Language > RUST' 카테고리의 다른 글

Rust 함수 동작 원리  (0) 2024.10.22
[Rustling] exercises/01_variables/variables6.rs 풀기  (0) 2024.10.11
[Restling] exercises/01_variables/variables4.rs 풀기  (0) 2024.10.11
[Rustling] exercises/01_variables/variables3.rs 풀기  (0) 2024.10.11
[Rustling] exercises/01_variables/variables2.rs 풀기  (0) 2024.10.11
'Programming Language/RUST' 카테고리의 다른 글
  • Rust 함수 동작 원리
  • [Rustling] exercises/01_variables/variables6.rs 풀기
  • [Restling] exercises/01_variables/variables4.rs 풀기
  • [Rustling] exercises/01_variables/variables3.rs 풀기
602zzang
602zzang
  • 602zzang
    yks_STUDY
    602zzang
  • 전체
    오늘
    어제
    • 분류 전체보기 (77)
      • Programming Language (36)
        • C (15)
        • PYTHON (9)
        • RUST (12)
      • Reverse Engineering (3)
      • OS (17)
        • LINUX (17)
      • 보안 이슈 (6)
      • Digital Forensics (1)
      • CTF (8)
      • 기타 (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    python
    파이썬
    TeamH4C
    cyberdefenders
    코드업
    빡공팟
    Rocky Linux
    보안동향
    picoCTF
    드림핵
    공급망
    bandit
    백준
    rustling
    c
    소켓 통신
    P4C
    rust
    umassctf2025
    보안이슈
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
602zzang
[Rustling] exercises/01_variables/variables5.rs 풀기
상단으로

티스토리툴바