Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Getting Started

Runnable examples covering the main ways to embed rust-meth-lib in your own tooling. Each file is self-contained and documents its own [dependencies] snippet.

basic_query.rs

This example first locates the rust-analyzer binary, then it queries the available methods on a RefCell<u8> and prints the results.

use rust_meth_lib::analyzer::{find_rust_analyzer, query_methods};

fn main() -> rust_meth_lib::error::Result<()> {
    let ra_path = find_rust_analyzer()?;

    let methods = query_methods("RefCell<u8>", &ra_path, None)?;

    println!("Methods on RefCell<u8> ({} total):", methods.len());
    for method in &methods {
        println!("  {}", method.name);
    }

    Ok(())
}

Output

The minimal end-to-end path: find_rust_analyzer → query_methods → print method names.

Methods on RefCell<u8> (30 total):
  as_ptr
  borrow
  borrow_mut
  clamp
  clone
  clone_from
  clone_into
  cmp
  eq
  ge
  get_mut
  gt
  into
  into_inner
  le
  lt
  max
  min
  ne
  partial_cmp
  replace
  replace_with
  swap
  take
  to_owned
  try_borrow
  try_borrow_mut
  try_borrow_unguarded
  try_into
  undo_leak

Limit Number of Methods Returned from Standard API

use rust_meth_lib::analyzer::{Method, find_rust_analyzer, query_methods};

fn main() -> rust_meth_lib::error::Result<()> {
    // Step 1: locate the rust-analyzer binary.
    //
    // find_rust_analyzer checks `PATH` first, then falls back to the rustup
    // component directory (~/.rustup/toolchains/<active>/bin/rust-analyzer).
    // Returns `RustMethError::RustAnalyzerNotFound` if neither exists.
    let ra_path = find_rust_analyzer()?;

    // Step 2: query methods.
    //
    // query_methods spins up an ephemeral LSP session, synthesises a
    // temporary Cargo project with `let _x: RefCell<u8> = todo!();`, triggers
    // a completion request at the dot position, and tears everything down.
    //
    // The third argument is `deps: Option<&str>`: `None` means stdlib only.
    let methods = query_methods("RefCell<u8>", &ra_path, None)?;

    let top5: Vec<Method> = methods.into_iter().take(5).collect();

    // println!("Methods on RefCell<u8> ({} total):", methods.len());
    println!("First 5 methods on RefCell<u8>:");
    // for method in &methods {
    for method in &top5 {
        println!("  {}", method.name);
    }

    Ok(())
}

Output

First 5 methods on RefCell<u8>:
  as_ptr
  borrow
  borrow_mut
  clamp
  clone

Using the Builder API

The same query using [MethodQuery], equivalent to the above but chainable:

use rust_meth_lib::analyzer::find_rust_analyzer;
use rust_meth_lib::query::MethodQuery;

fn main() -> rust_meth_lib::error::Result<()> {
    let ra_path = find_rust_analyzer()?;
    let methods = MethodQuery::new("RefCell<u8>")
        .run(&ra_path)?;

    println!("Methods on RefCell<u8> ({} total):", methods.len());
    for method in &methods {
        println!("  {}", method.name);
    }
    Ok(())
}

The builder becomes more useful when combining options, adding a filter or resolving source locations without separate function calls:

let methods = MethodQuery::new("RefCell<u8>")
    .filter("borrow")
    .run(&ra_path)?;

With the filter above, the output is (I changed the print statement to match):

Methods on RefCell<u8> with borrow in their name (5 total):
  borrow
  borrow_mut
  try_borrow
  try_borrow_mut
  try_borrow_unguarded

Limit the Number of Methods Returned

You can use the .limit() method to return N methods. For example, a Vec<u8>, returns over 200 methods. To only return the first 10 sorted alphabetically:

use rust_meth_lib::analyzer::find_rust_analyzer;
use rust_meth_lib::query::MethodQuery;

fn main() -> rust_meth_lib::error::Result<()> {
    let ra_path = find_rust_analyzer()?;
    let methods = MethodQuery::new("Vec<u8>").limit(10).run(&ra_path)?;

    println!("First 10 methods on Vec<u8>:");
    for method in &methods {
        println!("  {}", method.name);
    }
    Ok(())
}

Output

First 10 methods on Vec<u8>:
  align_to
  align_to_mut
  allocator
  append
  array_windows
  as_array
  as_ascii
  as_ascii_unchecked
  as_chunks
  as_chunks_mut