Advertisement
cwchen

[Rust] default parameters

Aug 23rd, 2017
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.50 KB | None | 0 0
  1. use std::default::Default;
  2.  
  3. #[derive(Debug)]
  4. pub struct Parameter {
  5.     a: u32,
  6.     b: u32,
  7.     c: u32,
  8. }
  9.  
  10. // Set default values for Parameter struct
  11. impl Default for Parameter {
  12.     fn default() -> Self {
  13.         Parameter { a: 2, b: 4, c: 6}
  14.     }
  15. }
  16.  
  17. fn some_calc(p: Parameter) -> u32 {
  18.     let (a, b, c) = (p.a, p.b, p.c);
  19.     a + b + c
  20. }
  21.  
  22. fn main() {
  23.     // Set default values for p except c
  24.     let p = Parameter { c: 10, .. Parameter::default() };
  25.     println!("{}", some_calc(p));
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement