Skip to content

Documentation: write_fmt is unclear #15830

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
kornelski opened this issue Jul 20, 2014 · 6 comments · Fixed by #16187
Closed

Documentation: write_fmt is unclear #15830

kornelski opened this issue Jul 20, 2014 · 6 comments · Fixed by #16187

Comments

@kornelski
Copy link
Contributor

I'm trying to upgrade code that worked in 0.10/0.11-pre (implementation of fmt::Show):

write!(formatter.buf, "\\#{:02x}{:02x}{:02x}", self.r, self.g, self.b),

I've found a write_fmt method that sounds like what I need, but it's undocumented:

http://doc.rust-lang.org/core/fmt/struct.Formatter.html#method.write_fmt

Linked Arguments documentation suggests usnig format_args!():

http://doc.rust-lang.org/core/fmt/struct.Arguments.html

but the usage I'd expect:

formatter.write_fmt(format_args!("\\#{:02x}{:02x}{:02x}", self.r, self.g, self.b)),

fails with "format argument must be a string literal.", which left me lost. If really supports only literals as arguments then it's totally useless, but if I'm doing something wrong, then the docs/compiler don't help me figure this out.

@bluss
Copy link
Member

bluss commented Jul 20, 2014

The docs are not up to date. I think this should work for you:

write!(formatter, "#{:02x}{:02x}{:02x}", self.r, self.g, self.b)

# doesn't need to be escaped anymore, and you can use write! directly on the formatter. It should have been documented at http://doc.rust-lang.org/std/fmt/index.html but it has not quite been updated.

*Updated to not ignore the return value. write! has a return value -- you can return it from the fmt method.

@kornelski
Copy link
Contributor Author

That works, thanks!

Shall I leave the issue open as a reminder to update the docs?

@sfackler
Copy link
Member

Errors from the formatter should be propagated upstream instead of being ignored.

try!(write!(formatter, "#{:02x}{:02x}{:02x}", self.r, self.g, self.b))

@steveklabnik
Copy link
Member

@pornel yes, please leave it open, i will tag it 'docs'

@steveklabnik
Copy link
Member

I just opened a PR to remove that faulty paragraph.

@huonw
Copy link
Member

huonw commented Aug 2, 2014

The docs are up-to-date, it's just format_args passes the created structure into a closure/function passed as the first argument. See here for more details.

steveklabnik added a commit to steveklabnik/rust that referenced this issue Aug 4, 2014
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 16, 2023
…on-extension-to-call-rust-analyzer, r=davidbarsky

code: expose workspaces to other extensions; remove `addProject` command

This (mostly red) PR does three things:
- Exposes two methods to companion extensions (`setWorkspaces` and `notifyRustAnalyzer`).
    - `setWorkspaces` is needed to update `linkedProjects` _without_ writing workspace/global configuration.
    - `notifyRustAnalyzer` to get the server to pull the new configuration.
- Makes `Ctx` implement `RustAnalyzerExtensionApi` to prevent accidental regressions.
- Remove `rust-analyzer.addProject`, as that will live in a buck2 companion extension. No need for that to be in rust-analyzer!

I can see the utility of combining `notifyRustAnalyzer` and `setWorkspaces` into a single method (`updateWorkspacesAndNotify()`?), but I don't feel strongly about this. My feeling is that this API could be easily changed in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants