typestar

String y &str en Rust

El String dueño de sus datos frente al &str prestado.

fn main() {
    let mut owned = String::from("type");
    owned.push_str("star");
    owned.push('!');

    let borrowed: &str = &owned;
    let upper = borrowed.to_uppercase();
    let parts: Vec<&str> = "a,b,c".split(',').collect();
    println!("{owned} {upper} {}", parts.len());
}

Cómo funciona

  1. String crece: push_str y push.
  2. &str presta una vista hacia él.
  3. Métodos como to_uppercase y split devuelven datos nuevos.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
10
Caracteres a escribir
257
Tokens
88
Ritmo de tres estrellas
95 tpm

Al ritmo de tres estrellas de 95 tokens por minuto, este intento toma unos 56 segundos.

Escribe este fragmento

Paso 3 de 4 en Slices y cadenas; paso 9 de 15 en Propiedad y préstamos.

← Anterior Siguiente →