Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A helper function to calculate the width should be used in fmt funcs #9

Open
@songzhi

Description

@songzhi

What Rust std::fmt does:

Acording to thisdoc,if I want to print some content which is around with spaces,I should write like this.println!("Hello {:5}!", "x");

But actually it has bugs when dealing with like CJK chars.I suppose it is because Rust thinkwidth aschars().count().So when I want to print a 10-chars -width line,it actually prints longer.Because CJK-like char counts 1 char but displays 2-chars-width.

What I want:

Here's my solution,It's simple but works:

let total_displayed_width =10;let chars_count ="你好".chars().count();let content_displayed_width =UnicodeWidthStr::width("你好");let width_in_formatter = total_displayed_width -(content_displayed_width - chars_count);print!("{}{:^width$}{}",'|',"你好",'|', width = width_in_formatter);// It prints '|   你好   |',perfectly 10-chars-width

Of course we can do it personally,but I think it is better this crate could provide a func like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp