AnyCompiler

Strings

std::string is a real owned, mutable string type — concatenating with + builds a new string, and .length() reports its size. Unlike Rust's split between an owned String and a borrowed &str, C++ just has the one string type, passed by value or by reference as needed.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string first = "Hello";
    string second = "World";
    string combined = first + ", " + second + "!";
    cout << combined << endl;
    cout << "length = " << combined.length() << endl;
    return 0;
}
Output
Hello, World!
length = 13

Substrings

s.substr(start, length) returns a new string containing length characters beginning at index start.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "C++ Programming";
    string sub = s.substr(4, 11);
    cout << sub << endl;
    return 0;
}
Output
Programming

Next: structs and classes — grouping related data, and the one difference between struct and class.

Practice exercise

Given string s = "rust lang", use substr to extract just "rust" (the first 4 characters), then print it.