← All Exercises

Rust Ownership Transfer

basicrust~15min

Background

Ownership is Rust core memory management mechanism, ensuring safety without garbage collection.

Problem

Write a Rust function that takes ownership of a String, returns its length, and demonstrate borrowing with references.

Input

A String value to analyze.

Output

The length of the string, with the original string consumed or borrowed.

Sample tests

Sample 1

Input: "hello"
Expected: 5

Your solution

Learning objective

Understand ownership transfer, borrowing with references, and the difference between move and borrow.