inflearn logo

Elegant Rust Intermediate (3) - Strings

Have you ever encountered a different string length with Korean text in Rust, or had code that slices a string panic? In this course, visual materials and runnable examples explain everything from the ownership relationship between `String` and &str to the difference between characters and bytes in UTF-8. You’ll practice creating, searching, splitting, and converting strings, as well as Unicode normalization, and learn the criteria for designing safe string APIs while reducing unnecessary copies.

15 learners are taking this course

Level Intermediate

Course period Unlimited

Rust
Rust
unicode
unicode
Rust
Rust
unicode
unicode

What you will gain after the course

  • The ability to understand the differences between String, &String, and &str and choose the appropriate type for the situation

  • The ability to distinguish between the number of characters and the number of bytes in UTF-8 and safely truncate strings

  • The ability to create, modify, search, split, and combine strings using standard APIs

  • The ability to convert strings and numbers/dates and appropriately handle invalid input

  • The ability to normalize and compare different Unicode representations of Hangul

Handling Rust Strings Properly, from Ownership to UTF-8

Strings are used in almost every program, including for user input, file paths, JSON fields, and error messages. In Rust, you need to distinguish between String, which owns the string, and &str, which borrows part of a string.

In this lecture, you will understand the memory structure and ownership relationships of String·&str, and learn how to process UTF-8 strings containing Korean characters without breaking character boundaries. You will explore string creation, modification, searching, splitting, and joining, as well as number and date conversion, and Korean Unicode normalization through executable examples.

You’ll also learn the principle of borrowing input whenever possible and creating a String only when a new string is needed. By the end of the course, you’ll be able to design string-processing functions that are safe and convenient to call.

What you'll learn

String and &str: The Principles of Ownership and Borrowing

We examine what String, &String, and &str each refer to from the perspective of memory structure and ownership. After reviewing slices of arrays and Vec, you'll learn how to borrow the entire string or parts of it as &str without copying.

We also compare with concrete examples which types should be used for functions that only read input, functions that store strings, and functions that return parts of the original.

The structure of slices that borrow a portion of the original data by specifying a starting position and length without copying the data

Understanding UTF-8 and Safely Handling Strings

The length of a Rust string represents the number of bytes, not the number of characters. Through strings containing both English and Korean characters, we'll examine the differences between len, chars, and bytes, and understand why panics can occur when slicing strings by byte index.

Next, you will practice creating, modifying, searching, splitting, and joining strings; formatting; converting numbers and dates; and Unicode normalization for Korean. Finally, you will implement and test functions for trimming whitespace, extracting the beginning of a string while preserving UTF-8 boundaries, splitting configuration strings, and masking sensitive words.

Why the Number of Characters and Bytes Differ

Things to Note Before Taking the Course

Practice Environment

  • You can practice on Windows, macOS, and Linux.

  • The latest Rust stable toolchain and Cargo are required.

  • Please prepare an editor such as VS Code, Zed, etc. that can edit Rust code.

  • The examples use Rust 2024 edition.

  • Cargo will download the crates needed for the date and Unicode normalization examples, so an internet connection is required for the initial build.

  • No special hardware or high-end PC specifications are required.


Learning Materials

  • Online Textbook

  • GitHub source code

Prerequisites and Notes

  • You need to know basic Rust syntax such as variables, functions, conditionals, and loops.\

  • You should understand the basic concepts of ownership and borrowing.


  • It would be helpful to have experience using Vec and slices.


  • We recommend taking the "Introduction to the Elegant Programming Language Rust" course.

Recommended for
these people

Who is this course right for?

  • A developer who has learned Rust’s ownership but is confused about when to use String and &str

  • A developer who encountered unexpected results and panics when calculating the length of or slicing Korean strings

  • Developers who habitually clone strings or create new String objects when processing strings

  • A developer who wants to safely handle strings coming from external sources, such as user input, file contents, and configuration values

Need to know before starting?

  • Understanding the Basics of Rust Syntax

  • Understanding Rust’s Memory Ownership Concept

Hello
This is hatemogi

Career Verified

2,169

Learners

171

Reviews

27

Answers

4.7

Rating

9

Courses

I am a backend developer with a strong interest in functional programming. I have led development teams as a development team manager at Kakao, a principal engineer at NHN, and a lead at LINE+. I also worked as the head of the development team at an AI startup called Dnotitia, where I worked on projects using Rust.

Curriculum

All

14 lectures ∙ (1hr 19min)

Published: 
Last updated: 

Reviews

Not enough reviews.
Please write a valuable review that helps everyone!

hatemogi's other courses

Check out other courses by the instructor!

Limited time deal ends in 8 days

$176,503.00

33%

$9.90