Lemmy
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
amargo85@infosec.pub to Rust@programming.devEnglish · 2 years ago

Rust, implemented in something real

chat-to.dev

external-link
message-square
2
link
fedilink
-23
external-link

Rust, implemented in something real

chat-to.dev

amargo85@infosec.pub to Rust@programming.devEnglish · 2 years ago
message-square
2
link
fedilink
Create a struct in Rust called Statistics that holds a list of numbers and implement methods to calculate these statistics. For example, you could have methods to calculate the mean, median and mode of the numbers stored in the structure. **The code** ```rust use std::collections::HashMap; struct Statistics { numbers: Vec, } impl Statistics { fn new(numbers: Vec) -> Self { Statistics { numbers } } fn mean(&self) -> Option { let sum: f64 = self.numbers.iter().sum(); let count = self.numbers.len() as f64; if count > 0 { Some(sum / count) } else { None } } fn median(&mut self) -> Option { self.numbers.sort_by(|a, b| a.partial_cmp(b).unwrap()); let len = self.numbers.len(); if len == 0 { return None; } if len % 2 == 0 { let mid = len / 2; Some((self.numbers[mid - 1] + self.numbers[mid]) / 2.0) } else { Some(self.numbers[len / 2]) } } fn mode(&self) -> Option { let mut map = HashMap::new(); for &num in &self.numbers { *map.entry(num).or_insert(0) += 1; } let max_value = map.values().max()?; let mode = map.iter().find(|&(_, &count)| count == *max_value)?; Some(*mode.0) } } fn main() { let numbers = vec![1.0, 2.0, 3.0, 4.0, 5.0, 5.0, 6.0, 6.0, 6.0]; let stats = Statistics::new(numbers.clone()); println!("Numbers: {:?}", numbers); println!("Mean: {:?}", stats.mean()); println!("Median: {:?}", stats.median()); println!("Mode: {:?}", stats.mode()); } ``` This code creates the struct Statistics, which has a vector of numbers. It implements methods to calculate the mean, median and mode of these numbers. The mean method calculates the mean of the numbers, median calculates the median and mode calculates the mode. Note that these methods return Option, as they may not be applicable (for example, if the array of numbers is empty).
  • autokludge@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 years ago

    deleted by creator

Rust@programming.dev

rust@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !rust@programming.dev

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits
  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 11 users / day
  • 100 users / week
  • 327 users / month
  • 1.49K users / 6 months
  • 1 local subscriber
  • 7.94K subscribers
  • 1.05K Posts
  • 4.54K Comments
  • Modlog
  • mods:
  • snowe@programming.dev
  • Ategon@programming.dev
  • torcherist@programming.dev
  • EdTheLegendary@programming.dev
  • UI: unknown version
  • BE: 0.19.13
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org