pub fn encrypt(text: &str) -> String {
    let text = text.to_lowercase()
        .chars()
        .filter(|c| c.is_lowercase())
        .collect::<Vec<_>>();

    let size = (text.len() as f64).sqrt().ceil() as usize;

    (0..size)
        .map(|r| {
            (0..size)
                .filter_map(|c| text.get(c * size + r))
                .collect::<String>()
        })
        .collect::<Vec<_>>()
        .join(" ")
}

pub fn encrypt(text: &str) -> String {
    let text = text.to_lowercase()
        .chars()
        .filter(|c| c.is_lowercase())
        .collect::<Vec<_>>();

    if text.is_empty() {
        return String::new();
    }

    let rows = (text.len() as f64).sqrt() as usize;
    let cols = (text.len() as f64 / rows as f64).ceil() as usize;

    let mut encrypted = String::with_capacity(rows * cols);

    for r in 0..cols {
        for c in 0..cols {
            let index = c * cols + r;

            if index < text.len() {
                encrypted.push(text[index]);
            }
        }

        if r < cols - 1 {
            encrypted.push(' ');
        }
    }

    encrypted
}

--- 
+++ 
@@ -4,28 +4,14 @@
         .filter(|c| c.is_lowercase())
         .collect::<Vec<_>>();
 
-    if text.is_empty() {
-        return String::new();
-    }
+    let size = (text.len() as f64).sqrt().ceil() as usize;
 
-    let rows = (text.len() as f64).sqrt() as usize;
-    let cols = (text.len() as f64 / rows as f64).ceil() as usize;
-
-    let mut encrypted = String::with_capacity(rows * cols);
-
-    for r in 0..cols {
-        for c in 0..cols {
-            let index = c * cols + r;
-
-            if index < text.len() {
-                encrypted.push(text[index]);
-            }
-        }
-
-        if r < cols - 1 {
-            encrypted.push(' ');
-        }
-    }
-
-    encrypted
+    (0..size)
+        .map(|r| {
+            (0..size)
+                .filter_map(|c| text.get(c * size + r))
+                .collect::<String>()
+        })
+        .collect::<Vec<_>>()
+        .join(" ")
 }
Improve solution for Crypto Square
