From f42c2cf897b00ce49d7fac9cfba61584cf4808fb Mon Sep 17 00:00:00 2001 From: Ole Martin Ruud Date: Mon, 7 Jan 2019 03:06:22 +0100 Subject: [PATCH] Format Rust code using rustfmt --- contents/bogo_sort/code/rust/bogosort.rs | 6 +++--- contents/bubble_sort/code/rust/bubble_sort.rs | 4 ++-- .../code/rust/euclidean_example.rs | 1 - .../code/rust/monte_carlo.rs | 5 ++++- contents/tree_traversal/code/rust/tree.rs | 14 ++++++++++---- contents/verlet_integration/code/rust/verlet.rs | 10 ++++++++-- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/contents/bogo_sort/code/rust/bogosort.rs b/contents/bogo_sort/code/rust/bogosort.rs index e2a726998..78c12323f 100644 --- a/contents/bogo_sort/code/rust/bogosort.rs +++ b/contents/bogo_sort/code/rust/bogosort.rs @@ -4,16 +4,16 @@ extern crate rand; use rand::{thread_rng, Rng}; -fn is_sorted(arr : &[i32]) -> bool { +fn is_sorted(arr: &[i32]) -> bool { for i in 1..arr.len() { - if arr[i-1] > arr[i] { + if arr[i - 1] > arr[i] { return false; } } true } -fn bogo_sort(arr : &mut [i32]) { +fn bogo_sort(arr: &mut [i32]) { while !is_sorted(arr) { thread_rng().shuffle(arr); } diff --git a/contents/bubble_sort/code/rust/bubble_sort.rs b/contents/bubble_sort/code/rust/bubble_sort.rs index d17718b4f..104e43783 100644 --- a/contents/bubble_sort/code/rust/bubble_sort.rs +++ b/contents/bubble_sort/code/rust/bubble_sort.rs @@ -1,7 +1,7 @@ extern crate rand; // External crate that provides random number generation tools -use rand::{thread_rng, Rng}; // Used for random number generation use rand::distributions::Uniform; // Used for a uniform distribution +use rand::{thread_rng, Rng}; // Used for random number generation fn bubble_sort(a: &mut [u32]) { let n = a.len(); @@ -24,4 +24,4 @@ fn main() { println!("Before sorting: {:?}", rand_vec); bubble_sort(&mut rand_vec); println!("After sorting: {:?}", rand_vec); -} \ No newline at end of file +} diff --git a/contents/euclidean_algorithm/code/rust/euclidean_example.rs b/contents/euclidean_algorithm/code/rust/euclidean_example.rs index bcc0e2be8..89b55ba22 100644 --- a/contents/euclidean_algorithm/code/rust/euclidean_example.rs +++ b/contents/euclidean_algorithm/code/rust/euclidean_example.rs @@ -32,4 +32,3 @@ fn main() { println!("{}", chk1); println!("{}", chk2); } - diff --git a/contents/monte_carlo_integration/code/rust/monte_carlo.rs b/contents/monte_carlo_integration/code/rust/monte_carlo.rs index ed653689e..860cdfedf 100644 --- a/contents/monte_carlo_integration/code/rust/monte_carlo.rs +++ b/contents/monte_carlo_integration/code/rust/monte_carlo.rs @@ -26,5 +26,8 @@ fn monte_carlo(n: i64) -> f64 { fn main() { let pi_estimate = monte_carlo(10000000); - println!("Percent error is {:.3}%", (100.0 * (pi_estimate - PI).abs() / PI)); + println!( + "Percent error is {:.3}%", + (100.0 * (pi_estimate - PI).abs() / PI) + ); } diff --git a/contents/tree_traversal/code/rust/tree.rs b/contents/tree_traversal/code/rust/tree.rs index 37d4d4f0c..4f2e64450 100644 --- a/contents/tree_traversal/code/rust/tree.rs +++ b/contents/tree_traversal/code/rust/tree.rs @@ -23,7 +23,7 @@ fn dfs_recursive_postorder(n: &Node) { } fn dfs_recursive_inorder_btree(n: &Node) { - if n.children.len() == 2{ + if n.children.len() == 2 { dfs_recursive_inorder_btree(&n.children[1]); println!("{}", n.value); dfs_recursive_inorder_btree(&n.children[0]); @@ -46,7 +46,7 @@ fn dfs_stack(n: &Node) { } } -fn bfs_queue(n: &Node){ +fn bfs_queue(n: &Node) { let mut queue = VecDeque::new(); queue.push_back(n); @@ -58,14 +58,20 @@ fn bfs_queue(n: &Node){ fn create_tree(num_row: u64, num_child: u64) -> Node { if num_row == 0 { - return Node { children: vec![], value: 0 }; + return Node { + children: vec![], + value: 0, + }; } let children = (0..num_child) .map(|_| create_tree(num_row - 1, num_child)) .collect(); - Node { children, value: num_row } + Node { + children, + value: num_row, + } } fn main() { diff --git a/contents/verlet_integration/code/rust/verlet.rs b/contents/verlet_integration/code/rust/verlet.rs index e1518a223..490994da2 100644 --- a/contents/verlet_integration/code/rust/verlet.rs +++ b/contents/verlet_integration/code/rust/verlet.rs @@ -50,6 +50,12 @@ fn main() { let (time_vv, vel_vv) = velocity_verlet(5.0, -10.0, 0.01); println!("Time for original Verlet integration: {}", time_v); - println!("Time and velocity for Stormer Verlet integration: {}, {}", time_sv, vel_sv); - println!("Time and velocity for velocity Verlet integration: {}, {}", time_vv, vel_vv); + println!( + "Time and velocity for Stormer Verlet integration: {}, {}", + time_sv, vel_sv + ); + println!( + "Time and velocity for velocity Verlet integration: {}, {}", + time_vv, vel_vv + ); }