String
Part of the Fundamentals section of Coddy's Rust journey — lesson 7 of 75.
A char is a single character (For example: 1, 6, %, b, p, ., T, etc.)
The String type is a special type that consists of multiple chars.
To initialize a string value in a variable, enclose it within double quotation marks:
let s1 = "This is a string";In the above example, a string variable named s1 is initialized.
Unlike with numbers, when working with strings we need to be more careful with types.
If you want to explicitly declare a String type, you need to convert the string like this:
let s1: String = "This is a string".to_string();Challenge
BeginnerStore the string I am learning to code with Coddy! in a variable named coddy using a string literal (with double quotes)..
Be sure to store the exact string value with correct casing.
Cheat sheet
A char is a single character (e.g., 1, 6, %, b, p, ., T).
A String consists of multiple chars. Initialize strings using double quotation marks:
let s1 = "This is a string";To explicitly declare a String type, convert the string literal:
let s1: String = "This is a string".to_string();Try it yourself
fn main() {
// Type your code below
let coddy = ?
// Don't change the line below
println!("coddy = \"{}\"", coddy);
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input