Skip to content

Adopt more Rust idioms in the library #372

Description

@sappenin

Some places in the library don't conform to typical Rust idioms. For example, see Request...

pub trait Request<'a> {
    fn get_common_fields(&self) -> &CommonFields<'a>;
    fn get_common_fields_mut(&mut self) -> &mut CommonFields<'a>;
}

...which uses get_ prefix in non-idiomatic ways.

Standard Rust Accessor Naming Conventions

  • field(&self) -> &T: Immutable: reference accessor (no get_ prefix)
  • field_mut(&mut self) -> &mut T: Mutable reference accessor.
  • into_field(self) -> T: Consuming getter that takes ownership.
  • as_field(&self) -> &T or to_field(&self) -> T: Standard type-conversion accessors.

When get IS Idiomatic

The get prefix or method name is used in Rust under limited, specific circumstances:

  • Lookups: Methods that require arguments to search or retrieve an item, often returning an Option or Result (e.g., HashMap::get(&key) or Slice::get(index)).
  • Single-value wrappers: Smart pointers or concurrency primitives where obtaining the underlying value is the container's core operation (e.g., Cell::get() or RefCell::get_mut()).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions