rust-analyzer

repository·master·Indexed 12 days ago

https://github.com/rust-lang/rust-analyzer

A high-performance language server for Rust that implements the Language Server Protocol (LSP) to provide IDE features such as code completion, refactoring, diagnostics, and navigation. It integrates with tools like rustfmt, rustc, and clippy, and is compatible with editors including VS Code, Vim, Emacs, and Zed.

Tokens
70.9K
Snippets
224
Records
381
Agent score
97%

What's inside rust-analyzer

  1. What is rust-analyzer

    master

    rust-analyzer is a language server that provides IDE functionality for writing Rust programs. It implements the Language Server Protocol (LSP), allowing it to be used with any compatible editor such as VS Code, Vim, Emacs, or Zed.

    Key features include:

    • Navigation: go-to-definition, find-all-references.
    • Editing: refactorings, code completion.
    • Tooling Integration: integrated formatting (via rustfmt) and integrated diagnostics (via rustc and clippy).
  2. Overview of rust-analyzer features

    master

    rust-analyzer provides a comprehensive suite of IDE features for Rust development, including:

    • Code Completion: Includes automatic import insertion.
    • Navigation: Go to definition, implementation, and type definition; find all references; workspace symbol search.
    • Refactoring: Symbol renaming.
    • Information: Types and documentation on hover; inlay hints for types and parameter names.
    • Visuals: Semantic syntax highlighting.
    • Code Actions: A wide variety of assists (code actions) and the ability to apply suggestions from errors.
  3. Explore rust-analyzer features

    master

    rust-analyzer provides several core features to enhance your development experience:

    • Assists (Code Actions): Quick fixes and refactorings.
    • Diagnostics: Real-time error and warning reporting.
    • Editor Features: General IDE-like capabilities such as completion, hover information, and more.
  4. Use text-size for text offsets

    master

    The text-size library provides type-safe newtype wrappers for representing text offsets. Instead of using raw u32 or tuples, use these wrappers to distinguish between different types of text positions (e.g., byte offsets vs. character offsets) in your code.

    It provides wrappers for:

    • u32: Typically used for a single offset (like a byte offset).
    • (u32, u32): Typically used for a range or a specific position (like line and column).
  5. What is SmolStr and when to use it

    master

    A SmolStr is an immutable string type designed for efficient storage of small strings, such as tokens in a programming language. It is optimized to avoid heap allocations in several common scenarios:

    • Stack Allocation: Strings up to 23 bytes long are stored directly on the stack.
    • Whitespace Optimization: Strings longer than 23 bytes that consist solely of consecutive newlines followed by consecutive spaces (the WS pattern) are also stack-allocated.
    • Static Strings: SmolStr can be created from a &'static str without any allocation.
    • O(1) Cloning: Cloning a SmolStr is an $O(1)$ operation because it does not require a new allocation.

    Memory Footprint: On 64-bit platforms, size_of::<SmolStr>() is 24 bytes, which is identical to size_of::<String>().

    Note: If a string is longer than 23 bytes and does not follow the WS pattern, it will be heap-allocated. For use cases requiring heavy deduplication, a specialized interner may be more efficient.

  6. Understand rust-analyzer network activity and privacy

    master

    By default, rust-analyzer does not perform network access itself. However, it executes cargo metadata to resolve project dependencies, which may trigger the downloading or updating of the crate registry and dependency source code.

    Additionally, if enabled (which is the default behavior), build scripts and procedural macros executed during the analysis process have full system permissions and can perform network access.

  7. How the proc-macro server architecture works

    master

    rust-analyzer uses a client-server architecture via RPC (over stdio) to handle procedural macro expansion. This separation serves two purposes:

    1. Fault Tolerance: Proc macros are dynamic libraries that can cause segmentation faults. Running them in a separate process allows rust-analyzer to recover from fatal errors without crashing the entire IDE session.
    2. Version Compatibility: Proc macro dylibs are compiled against specific rustc versions and require matching internal APIs. By shipping this binary as a rustup component, the server can always match the current rustc version regardless of the rust-analyzer version being used.
  8. Using rust-analyzer with VS Code Live Share

    master

    rust-analyzer has partial support for VS Code Live Share:

    • Requirement: You must use the official Microsoft build of VS Code; OSS builds are not supported.
    • Behavior: The host's rust-analyzer instance is shared with all guests. Guests do not need the rust-analyzer extension installed.
    • Limitation: If a guest has rust-analyzer installed locally, command palette commands may fail because they will attempt to communicate with the guest's local server instead of the host's.
  9. Releasing new versions of library crates

    master

    Crates located in the lib/ directory are published to crates.io and follow semantic versioning (semver). To release a new version of a crate:

    1. Update the version number in the crate's Cargo.toml file.
    2. Submit a Pull Request.
    3. Once the PR is merged into master, an automated workflow will publish the new version to crates.io.

    Note: You must release a new version to crates.io before a new API can be used by the main rust-analyzer project, as it relies on the published versions.