Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,624 questions

56,911 answers

573 users

How to format a string using fixed-width fields in Rust

6 Answers

0 votes
/*
Common format specifiers
-----------------------------------
{:<10} // Left-align, width 10
{:>10} // Right-align, width 10
{:^10} // Center-align, width 10
{:08} // Width 8, pad with zeros
{:.2} // 2 decimal places
{:10.2} // Width 10, 2 decimal places
*/

/// Formats a record using fixed-width fields.
///
/// {:<15}  = left-align text in a field 15 characters wide
/// {:>10}  = right-align integer in a field 10 characters wide
/// {:>12.2} = right-align floating-point number in a field
///            12 characters wide with 2 decimal places
fn format_record(name: &str, age: u32, salary: f64) -> String {
    format!("{:<15}{:>10}{:>12.2}", name, age, salary)
}

fn main() {
    // Store the formatted string in a variable
    let result = format_record("Alice", 61, 215000.0);

    println!("{}", result);
}


/*
run:

Alice                  61   215000.00

*/

 



answered 3 days ago by avibootz
edited 3 days ago by avibootz
0 votes
fn main() {
    // Left-align "Hello" in a field 10 characters wide
    let result = format!("{:<10}", "Hello");

    println!("|{}|", result);
}


/*
run:

|Hello     |

*/

 



answered 3 days ago by avibootz
0 votes
// Right-aligned field

fn main() {
    // Right-align "Tom" in a field 10 characters wide
    let result = format!("{:>10}", "Tom");

    println!("|{}|", result);
}


/*
run:

|       Tom|

*/

 



answered 3 days ago by avibootz
0 votes
// Zero-padding numbers

fn main() {
    let number = 123;

    // Pad with leading zeros to width 8
    let result = format!("{:08}", number);

    println!("{}", result);
}


/*
run:

00000123

*/

 



answered 3 days ago by avibootz
0 votes
// Formatting a table

fn main() {
    // Print column headers
    println!("{:<15}{:<10}{:<12}", "Name", "Age", "Salary");

    // Print data rows
    println!("{:<15}{:<10}{:<12.2}", "Dana", 39, 78000.0);
    println!("{:<15}{:<10}{:<12.2}", "Laura", 48, 91000.0);
}


/*
run:

Name           Age       Salary      
Dana           39        78000.00    
Laura          48        91000.00    

*/

 



answered 3 days ago by avibootz
0 votes
/// Returns a string padded to a fixed width.
///
/// left_align = true  => left alignment
/// left_align = false => right alignment
fn fixed_width(value: &str, width: usize, left_align: bool) -> String {
    if left_align {
        format!("{:<width$}", value, width = width)
    } else {
        format!("{:>width$}", value, width = width)
    }
}

fn main() {
    println!("|{}|", fixed_width("Hello", 10, true));
    println!("|{}|", fixed_width("123", 10, false));
}


/*
run:

|Hello     |
|       123|

*/

 



answered 3 days ago by avibootz
...