Skip to content

Format Macro Named Argument Example is Confusing #115551

Closed
@thisisscotts

Description

@thisisscotts

Location

https://doc.rust-lang.org/std/macro.format.html#examples

Summary

I was sent here from the Rust website tracker. Please advise if I am posting in the wrong area.

What needs to be fixed?

format! macro documentation could be improved by changing one of the examples to be more obvious to new users.

Suggested Improvement

Change the example
format!("x = {}, y = {y}", 10, y = 30);
to
format!("x = {}, y = {z}", 10, z = 30);

Additionally, assert! could be used to demonstrate final string values:

let greeting = format!("hello {}", "world!");
assert!(greeting=="hello world!");
let xy = format!("x = {}, y = {z}", 10, z = 30);
assert!(xy=="x = 10, y = 30");
let (a, b) = (1, 2);
let ab = format!("{a} + {b} = 3");
assert!(ab=="1 + 2 = 3");

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions