Menu
Coddy logo textTech

문자열 비교

Coddy Rust 여정의 Fundamentals 섹션에 포함된 레슨 — 75개 중 19번째.

Rust에서 문자열을 생성하는 두 가지 주요 방법이 있습니다:

방법 1: 단순 문자열 리터럴 (str):

let str1 = "hello";
let str2 = "hello";
let str3 = "Hello";

이것들은 프로그램에 내장된 단순하고 고정된 문자열입니다.

방법 2: 전체 문자열 (String):

let string1: String = "hello".to_string();
let string2: String = String::from("hello");
let string3 = "hello".to_owned();

이것들은 나중에 변경할 수 있는 더 유연한 유형의 문자열을 생성합니다. 세 줄 모두 기본적으로 동일한 작업을 수행하며, 단지 작성하는 방법이 다를 뿐입니다.

String::from::는 타입에서 직접 연관 함수(associated function)를 호출하기 위한 Rust의 구문입니다. 이를 String 자체의 내장된 새로운 문자열 값 생성 방법이라고 생각하면 됩니다.

대부분의 문자열 비교에서 두 타입은 동일하게 작동합니다:

let result1 = str1 == str2;  // true
let result2 = str1 == str3;  // false (대소문자 구분)

// String과 String 비교
let result3 = string1 == string2;  // true

// String과 &str을 비교할 수도 있습니다
let result4 = string1 == str1;     // true

치트 시트

Rust에서 문자열을 생성하는 두 가지 주요 방법이 있습니다:

문자열 리터럴 (str):

let str1 = "hello";

전체 문자열 (String):

let string1: String = "hello".to_string();
let string2: String = String::from("hello");
let string3 = "hello".to_owned();

문자열 비교는 두 유형 모두에서 작동하며 대소문자를 구분합니다:

let result1 = str1 == str2;     // true
let result2 = string1 == str1;  // true (String과 &str)

직접 해보기

이 레슨에는 코드 챌린지가 없습니다.

quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Fundamentals의 모든 레슨