Rust, a modern systems programming language, is known for its focus on safety, performance, and concurrency. To effectively use Rust, developers must be familiar with its keywords and programming concepts. In this article, we will delve into the common programming concepts in Rust, focusing on keywords, their current functionality, and reserved keywords for potential future use.
Keywords Currently in Use
- as: Used to perform primitive casting, disambiguate the specific trait containing an item, or rename items in use statements.
- async: Indicates that a function will return a Future instead of blocking the current thread, enabling asynchronous programming.
- await: Suspends execution until the result of a Future is ready, facilitating asynchronous code execution.
- break: Exits a loop immediately, providing a way to break out of repetitive execution.
- const: Defines constant items or constant raw pointers, emphasizing immutability.
- continue: Skips to the next iteration of a loop, allowing for fine-grained control over loop execution.
- crate: In a module path, refers to the crate root, facilitating module organization and structure.
- dyn: Enables dynamic dispatch to a trait object, supporting polymorphism.
- else: Used as a fallback for if and if let control flow constructs.
- enum: Defines an enumeration, a custom data type with named values.
- extern: Links an external function or variable, enabling interaction with external libraries or system calls.
- false: Boolean false literal, representing the absence of truth.
- fn: Defines a function or the function pointer type, a fundamental building block of Rust programs.
- for: Used to loop over items from an iterator, implement a trait, or specify a higher-ranked lifetime.
- if: Branches based on the result of a conditional expression, a fundamental construct for decision-making.
- impl: Implements inherent or trait functionality, supporting code organization and reuse.
- in: Part of for loop syntax, indicating the iterable over which the loop iterates.
- let: Binds a variable, introducing a new variable into the current scope.
- loop: Creates an unconditional loop, repeating a block of code indefinitely until a break statement is encountered.
- match: Matches a value to patterns, offering a powerful and expressive way to handle different cases.
- mod: Defines a module, facilitating code organization and encapsulation.
- move: Makes a closure take ownership of all its captures, influencing the borrowing and ownership model.
- mut: Denotes mutability in references, raw pointers, or pattern bindings, distinguishing mutable from immutable.
- pub: Denotes public visibility in struct fields, impl blocks, or modules, specifying which items are accessible from outside the module.
- ref: Binds by reference, enabling the creation of references to values.
- return: Exits a function, optionally returning a value to the caller.
- Self: A type alias for the type being defined or implemented, enhancing code clarity and expressiveness.
- self: Refers to the method subject or current module, providing a concise way to access instance methods or module-level items.
- static: Declares a global variable or lifetime lasting the entire program execution, allowing for shared state.
- struct: Defines a structure, a composite data type with named fields.
- super: Refers to the parent module of the current module, aiding in module hierarchy navigation.
- trait: Defines a trait, a set of methods that can be implemented by types, facilitating code reuse through polymorphism.
- true: Boolean true literal, representing the presence of truth.
- type: Defines a type alias or associated type, enhancing code readability and abstraction.
- union: Defines a union, a type that can hold values of different types but only one at a time.
- unsafe: Denotes unsafe code, functions, traits, or implementations, allowing for operations that are not checked for safety by the compiler.
- use: Brings symbols into scope, simplifying code by avoiding fully qualified names.
- where: Denotes clauses that constrain a type, providing additional constraints in generic contexts.
- while: Loops conditionally based on the result of an expression, offering a fundamental looping construct.
Keywords Reserved for Future Use
The following keywords are reserved by Rust for potential future use and do not yet have any functionality:
- abstract
- become
- box
- do
- final
- macro
- override
- priv
- try
- typeof
- unsized
- virtual
- yield