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

Querying Methods

Shows how to work with the richer fields on [Method] beyond just the name:

  • Method::detail: the full function signature as a string (e.g. "const fn(&self) -> *mut T"), returned by rust-analyzer as the completion item’s detail field. Option<String>: not always present, especially for trait-provided methods on generic types.
  • Method::documentation: the rustdoc string for the method, often multi-line. Option<String>: coverage varies by crate and method.

Both fields are None when rust-analyzer doesn’t return them. Methods with neither still appear in the results from query_methods. This example filters to only those with at least one field populated, and renders them gracefully when the other is absent.

method_info.rs

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

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

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

    // Only print methods that have at least a signature. Methods with
    // neither detail nor docs still exist in the list, they're just
    // less useful for display purposes.
    let documented: Vec<&Method> = methods
        .iter()
        .filter(|m| m.detail.is_some() || m.documentation.is_some())
        .collect();

    println!(
        "{} of {} Cell<u8> methods have signatures or docs:\n",
        documented.len(),
        methods.len()
    );

    for method in documented {
        // The signature line (e.g. "pub fn push_str(&mut self, string: &str)")
        if let Some(sig) = &method.detail {
            println!("fn {}  →  {sig}", method.name);
        } else {
            println!("fn {} (no signature)", method.name);
        }

        // Rustdoc
        if let Some(doc) = &method.documentation {
            // Just the first sentence to keep output manageable.
            let first_line = doc.lines().next().unwrap_or("").trim();
            if !first_line.is_empty() {
                println!("  doc: {first_line}");
            }
        }

        println!();
    }

    Ok(())
}

Output

26 of 26 Cell<u8> methods have signatures or docs:

fn as_ptr  →  const fn(&self) -> *mut T
  doc: Returns a raw pointer to the underlying data in this cell.

fn clamp  →  fn(self, Self, Self) -> Self
  doc: Restrict a value to a certain interval.

fn clone  →  fn(&self) -> Self
  doc: Returns a duplicate of the value.

fn clone_from  →  fn(&mut self, &Self)
  doc: Performs copy-assignment from `source`.

fn clone_into  →  fn(&self, &mut <Self as ToOwned>::Owned)
  doc: Uses borrowed data to replace owned data, usually by cloning.

fn cmp  →  fn(&self, &Self) -> Ordering
  doc: This method returns an [`Ordering`] between `self` and `other`.

fn eq  →  fn(&self, &Rhs) -> bool
  doc: Equality operator `==`.

fn ge  →  fn(&self, &Rhs) -> bool
  doc: Tests greater than or equal to (for `self` and `other`) and is used by

fn get  →  const fn(&self) -> T
  doc: Returns a copy of the contained value.

fn get_mut  →  const fn(&mut self) -> &mut T
  doc: Returns a mutable reference to the underlying data.

fn gt  →  fn(&self, &Rhs) -> bool
  doc: Tests greater than (for `self` and `other`) and is used by the `>`

fn into  →  fn(self) -> T
  doc: Converts this type into the (usually inferred) input type.

fn into_inner  →  const fn(self) -> T
  doc: Unwraps the value, consuming the cell.

fn le  →  fn(&self, &Rhs) -> bool
  doc: Tests less than or equal to (for `self` and `other`) and is used by the

fn lt  →  fn(&self, &Rhs) -> bool
  doc: Tests less than (for `self` and `other`) and is used by the `<` operator.

fn max  →  fn(self, Self) -> Self
  doc: Compares and returns the maximum of two values.

fn min  →  fn(self, Self) -> Self
  doc: Compares and returns the minimum of two values.

fn ne  →  fn(&self, &Rhs) -> bool
  doc: Inequality operator `!=`.

fn partial_cmp  →  fn(&self, &Rhs) -> Option<Ordering>
  doc: This method returns an ordering between `self` and `other` values if one exists.

fn replace  →  const fn(&self, T) -> T
  doc: Replaces the contained value with `val`, and returns the old contained value.

fn set  →  const fn(&self, T)
  doc: Sets the contained value.

fn swap  →  fn(&self, &Cell<T>)
  doc: Swaps the values of two `Cell`s.

fn take  →  const fn(&self) -> T
  doc: Takes the value of the cell, leaving `Default::default()` in its place.

fn to_owned  →  fn(&self) -> <Self as ToOwned>::Owned
  doc: Creates owned data from borrowed data, usually by cloning.

fn try_into  →  fn(self) -> Result<T, <Self as TryInto<T>>::Error>
  doc: Performs the conversion.

fn update  →  const fn(&self, impl FnOnce(T) -> T)
  doc: Updates the contained value using a function.