Rust: What the &()::||_’*+! is this?

While many of the symbols used in rust source code are like those used in other languages, some languages do not use those symbols or use them differently. This blog post provides short explanations for rust programming symbols that may not be used or may be used differently in other languages. Again, I am just learning rust, and although this might not be completely technically accurate or use perfect terminology, it could assist casual rust readers. Real developers should of course consult the documentation for a more thorough explanation and greater understanding of not only the symbols, but the concepts that they represent.

What follows are simplified explanations of my understanding of some common symbols in rust source code.

  • &
    Borrows a reference, which has to do with managing the lifetime of its data, often when passing things to functions.

  • Designates lifetime requirements explicitly, typically to help the compiler validate arguments to functions.
  • !
    In addition to being the negation (not) operator, following what appears to be a function name, ! indicates a macro, such as println!(), which works something like a function defined with the fn token. This can be particularly confusing in statements such as the following, which, especially considering that rust conditionals do not require parentheses, one might assume negates the condition check: pub fn is_windows() -> bool { cfg!(target_os = “windows”)}.
  • _
    Catchall condition for match expressions, which are something like big switch statements, but much better.
  • *
    Dereference or raw pointer. I try not to do this.
  • ::
    This is typically a scope resolution operator in rust paths, such as such as std::io, which are similar to namespaces and typically correspond to file paths, often in external packages.
  • ||
    With contents, this can indicate a closure.
  • ()
    Without a name and with contents, these characters () symbol generally seems to indicate a tuple. After a name, they can indicate an enumeration, which in rust is a construct that can contain a value within those parentheses. An empty pair of parentheses can also basically indicate a null set, which seems similar to the Some() and None enumeration pattern.
  • ?
    Following certain statements, this can propagate an error up the calls stack. This allows exception management at a higher level without try/catch blocks and for local code to proceed as if no error had occurred.
  • +
    Used to indicate constraints in type conditions.

Note that there appears to be no ++ operator in rust.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: