Curriculum Ownership & borrowing Strings and slices
Strings and slices
Rust has owned strings (String) and borrowed string slices (&str). The same borrowed-view idea applies to slices like &[T]. This theme teaches the signature reflex: accept &str or &[T] when you only read, return String or Vec<T> when you create owned data. Planned exercises: 1. choose &str for a read-only parameter. 2. choose String for owned output. 3. recognise slice syntax. 4. fill .to_string(). 5. fill a slice parameter. 6. write a string-reading helper. 7. write a slice-summing helper. 8. translate TS string code. 9. fix a function that returns a reference to a temporary string.
Exercises
9 ready
01
pick one
A function only READS a string — measures, prints, returns a new
02
pick one
A function builds and returns a brand-new string (e.g. "@" + name).
03
pick one
Given the source on the side, what is the type of first and
04
fill blanks
Fill the method that converts a &str literal into an owned
05
fill blanks
Fill the parameter type: a SHARED VIEW into a sequence of i32s.
06
type one line
Write the signature for len_of. It takes a string slice and
07
write a program
Write a Rust program with a helper sum_slice that takes a
08
write a program
Translate the TypeScript string helper to Rust. shout takes a
09
write a program
This program fails to compile: