Skip to content

Format Macro Named Argument Example is Confusing #115551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thisisscotts opened this issue Sep 5, 2023 · 3 comments · Fixed by #115560
Closed

Format Macro Named Argument Example is Confusing #115551

thisisscotts opened this issue Sep 5, 2023 · 3 comments · Fixed by #115560
Assignees
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@thisisscotts
Copy link

thisisscotts commented Sep 5, 2023

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");
@thisisscotts thisisscotts added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Sep 5, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 5, 2023
@ShE3py
Copy link
Contributor

ShE3py commented Sep 5, 2023

Something like

assert_eq!(format!("x = {}, y = {val}", 10, val = 30), "x = 10, y = 30");

?

@rustbot claim

@thisisscotts
Copy link
Author

@ShE3py I think the use of "val" certainly makes it more obvious than my suggestion to use "z". Your suggestion would also do it in one line... I guess the question is whether you would prefer to make it more obvious (readable) by splitting it into two lines.

This is my first contribution to Rust in any form, so I'm happy to go with your suggestion.

@fmease fmease added T-libs Relevant to the library team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 6, 2023
@bors bors closed this as completed in 633f143 Sep 16, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 16, 2023
Rollup merge of rust-lang#115560 - ShE3py:format-results, r=dtolnay

Update doc for `alloc::format!` and `core::concat!`

Closes rust-lang#115551.

Used comments instead of `assert!`s as [`std::fmt`](https://doc.rust-lang.org/std/fmt/index.html#usage) uses comments.

Should all the str-related macros (`format!`, `format_args!`, `concat!`, `stringify!`, `println!`, `writeln!`, etc.) references each others? For instance, [`concat!`](https://doc.rust-lang.org/core/macro.concat.html) mentions that integers are stringified, but don't link to `stringify!`.

`@rustbot` label +A-docs +A-fmt
@thisisscotts
Copy link
Author

Thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants