typestar

Elision rules in Rust

Most signatures need no annotation, because three rules infer the obvious case.

struct Snippet {
    text: String,
}

impl Snippet {
    fn first_word(&self) -> &str {
        self.text.split_whitespace().next().unwrap_or("")
    }

    fn lines(&self) -> impl Iterator<Item = &str> {
        self.text.lines()
    }
}

fn trimmed(text: &str) -> &str {
    text.trim()
}

fn main() {
    let s = Snippet { text: "let x = 1;\nlet y = 2;".to_string() };
    println!("{}", s.first_word());
    println!("{}", s.lines().count());
    println!("{:?}", trimmed("  padded  "));
}

How it works

  1. One input reference means the output borrows from it.
  2. A method with &self borrows from self by default.
  3. Ambiguity is the only case that needs writing out.

Keywords and builtins used here

The run, in numbers

Lines
24
Characters to type
437
Tokens
150
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 86 seconds.

Type this snippet

Step 3 of 4 in Lifetime annotations, step 3 of 11 in Lifetimes & interior mutability.

← Previous Next →