Strings and &str
Owned and borrowed text.
Strings and &str is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two String Types
Rust has two main text types:
String— an owned, growable string.&str— a borrowed string slice, a view into text.
fn main() {
let owned: String = String::from("hello");
let borrowed: &str = "world";
println!("{} {}", owned, borrowed);
}String Literals Are &str
When you write text in double quotes, you get a &str. It is fixed and stored in the program binary.
fn main() {
let greeting = "Hi there"; // type is &str
println!("{}", greeting);
}Creating a String
Build an owned String with String::from or the .to_string() method.
fn main() {
let a = String::from("abc");
let b = "xyz".to_string();
println!("{} {}", a, b);
}Growing a String
Because String is owned and growable, you can append to it with push_str and push.
fn main() {
let mut s = String::from("Hello");
s.push_str(", ");
s.push_str("Rust");
s.push('!');
println!("{}", s);
}Concatenation
Join strings using the + operator or the format! macro. format! does not consume its inputs.
fn main() {
let first = String::from("Hello");
let second = String::from("World");
let joined = format!("{} {}", first, second);
println!("{}", joined);
}Borrowing a String as &str
A String can be borrowed as a &str automatically. This lets functions accept either type.
fn print_text(text: &str) {
println!("text: {}", text);
}
fn main() {
let owned = String::from("owned");
print_text(&owned);
print_text("literal");
}Why Prefer &str Parameters
Functions usually take &str rather than String. This is more flexible: callers can pass owned strings, literals, or slices.
fn shout(s: &str) -> String {
s.to_uppercase()
}
fn main() {
println!("{}", shout("quiet"));
}Length in Bytes
The len() method returns the length in bytes, not characters. ASCII text has one byte per character.
fn main() {
let s = "hello";
println!("{} bytes", s.len());
}Checking Content
Useful read methods include contains, starts_with, and is_empty.
fn main() {
let s = "hello world";
println!("{}", s.contains("world"));
println!("{}", s.starts_with("hello"));
}Ownership Difference
A String owns its data on the heap and is responsible for freeing it. A &str just borrows existing data and owns nothing.
fn main() {
let owned = String::from("data");
let view: &str = &owned[0..2];
println!("owned {} view {}", owned, view);
}Converting Between Them
Convert back and forth:
&strtoString:.to_string()orString::from.Stringto&str:&sors.as_str().
fn main() {
let s = String::from("convert");
let slice: &str = s.as_str();
let back: String = slice.to_string();
println!("{} {}", slice, back);
}Quick Check
Recall the difference between the two text types.
Recap
Strings in Rust:
String— owned, growable, heap-allocated.&str— borrowed slice / view, what literals are.- Create with
String::fromorto_string(); grow withpush_str. - Prefer
&strfor function parameters.
fn main() {
let mut msg = String::from("Rust");
msg.push_str(" rocks");
let view: &str = &msg;
println!("{} ({} bytes)", view, view.len());
}Frequently asked questions
Is the “Strings and &str” lesson free?
Yes — the full text of “Strings and &str” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Strings and &str”?
Owned and borrowed text. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Strings and &str” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Vectors
- Strings and &str
- HashMaps
- Iterating Collections