Rust, 쉽게 하자!

Rust 입문

[Rust] println! 매크로

바로크냥 2022. 9. 23. 06:04

7. println! 매크로

이 매크로는 문자열을 출력합니다.

변수를 출력할 때는 보간법을 사용합니다.

 

 let a = 100;
 let b = "사과";
 println!("{} {}개", b, a);

// 출력
사과 100개

 

보간 사용법을 몇 가지 더 살펴 보겠습니다.

 

// 1

  println!("{0} {1} {2}", "apple", "grape", "orange"); 

// 출력

apple grape orange



  // 2
  println!("{0} {1} {0} {1}", "apple", "grape");  
// 출력
apple grape apple grape

  // 3
  println!("{apple} {grape} {orange}", apple = "red", grape = "purple", orange = “orange");
// 출력
red purple orange

  // 4
  println!("{w} {}", "world!!!", w = "hello");
// 출력
hello world!!!

// 1: 문자열 순서대로 0, 1, 2 …의 인덱스 번호로 불러 올 수 있습니다.

// 2: 인덱스로 불러 올 때는 반복 해서 사용 할 수 있습니다.

// 3: 문자열에 이름을 붙여서 사용 할 수 있습니다.

// 4: 이름 없는 문자열과 이름 있는 문자열을 함께 사용할 때는 이름 있는 문자열을 제일 뒤로 보내야 합니다. 앞에 두면 에러가 발생합니다. 

 
반응형

'Rust 입문' 카테고리의 다른 글

[Rust] 변수, 상수 그리고 static  (0) 2022.09.25
[Rust] 데이터 타입  (0) 2022.09.25
[Rust] 주석 달기  (1) 2022.09.23
[Rust] main()을 알아보자  (1) 2022.09.23
[Rust] Cargo.toml을 알아보자  (1) 2022.09.23