Skip to content

Why do functions of trait Statistics take ownership on the dataset and not immutable reference ? #345

Closed

Description

@DvxBellorvm

Hello

This is a question I have been wondering for a while: why all the functions in the statistics::Statistics trait take self and not &self ?
I often have the use case where I need to compute several statistics on the same dataset, like for example:

use statrs::statistics::Statistics;

fn main() {
    let dataset = vec![0., 1., 2., 3., 4., 5.];
    let mean = dataset.mean();
    let variance = dataset.variance();
}

but I cannot do so because mean(self) function takes ownership on the dataset. The only solution I see is to clone the dataset, which in my use case is usually not wanted.
Naïvely, if I had to implement a mean() function, I would only need an immutable reference on the dataset to do so, so I am wondering if there is a specific reason for this particular design choice, and could it change in the future ?

Activity

  1. YeungOnion commented on Jul 12, 2025

    @YeungOnion
    Contributor

    We could certainly change this so that only the rank/order related statistics have the ownership signature. We could also use an iterator trait bound, are you able to make the change?

  2. HuStmpHrrr commented on Sep 11, 2025

    @HuStmpHrrr

    the same question. it doesn't make sense to take the ownership of the data structure at all.

  3. YeungOnion commented on Aug 18, 2026

    @YeungOnion
    Contributor

    the online module provides an API circumventing this problem and also is more numerically stable. I planned on migrating the whole statistics API since there's some conflated aspects of the random variate statistics that don't play nicely. There's also work for comparative statistics like ks and mwu tests being worked in #406 to drop need for clone.

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