fork download
  1. fn f(current: i32, size: i32, width: i32) -> String {
  2. use std::cmp::{min, max};
  3. let b = min(max(1, current - width / 2), size - width + 1);
  4. let to_s = |p| if p == current {format!("[{}]", p)} else {format!("{}", p)};
  5. (b..b + width).map(to_s).collect::<Vec<_>>().join(" ")
  6. }
  7. fn main() {
  8. for (a, b, c) in vec!((1, 10, 5), (5, 10, 5), (10, 10, 5)) {println!("{:?}", f(a, b, c))}
  9. }
  10.  
Success #stdin #stdout 0s 14896KB
stdin
Standard input is empty
stdout
"[1] 2 3 4 5"
"3 4 [5] 6 7"
"6 7 8 9 [10]"