Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@evertrol
Created October 15, 2017 08:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evertrol/a0e3c94490724a1de9028a94dc0260e9 to your computer and use it in GitHub Desktop.
Save evertrol/a0e3c94490724a1de9028a94dc0260e9 to your computer and use it in GitHub Desktop.
/// Rust Fibonacci one-liner using `fold`
///
/// Not very practical: only for one time use,
/// since it will have to re-calculate all terms again and again
/// if used in a loop to create multiple items.
/// The use of `_` also suggests this is not an ideal solution.
fn main() {
let n = 10;
let x = (0..n).fold((0, 1), |x, _| (x.0+x.1, x.0)).0;
println!("{}: {}", n, x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment