Skip to content

docs: add amount example for units crate#43

Open
Zeegaths wants to merge 1 commit intorust-bitcoin:masterfrom
Zeegaths:docs-amount-example
Open

docs: add amount example for units crate#43
Zeegaths wants to merge 1 commit intorust-bitcoin:masterfrom
Zeegaths:docs-amount-example

Conversation

@Zeegaths
Copy link

@Zeegaths Zeegaths commented Mar 12, 2026

Add cookbook entry for Amount covering the "Amount" section from #4694.

What it covers:

  • Justifies the MAX = 21 million decision
  • Demonstrates creating amounts using constants (MAX, ONE_BTC, ONE_SAT, ZERO, FIFTY_BTC)
  • Explains result handling differences between from_sat_u32 and from_sat
  • Demonstrates parsing amounts from strings using parse
  • Covers formatting with display_in, display_dynamic, and the alloc-gated string methods
  • Shows basic NumOpResult usage with arithmetic and underflow handling

Closes the "Amount" section of #4694.

@apoelstra
Copy link
Member

Overall this looks amazing, thanks! I just commented a few nits.

@Zeegaths Zeegaths force-pushed the docs-amount-example branch from 1ede28e to e1c8a0c Compare March 15, 2026 13:04
@Zeegaths
Copy link
Author

Overall this looks amazing, thanks! I just commented a few nits.

Thanks for this! made the changes. I could pick up the rest of the docs?

@tcharding
Copy link
Member

Thanks for the contribution.

I could pick up the rest of the docs?

I'll review quite heavily then so that it saves you time on the next ones.

@@ -0,0 +1,168 @@
# Amount

In this section, we will demonstrate different ways of working with Bitcoin amounts using the `Amount` type. The examples in this section will:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to use little 'b' for the currency and big 'B' for the network.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

let large = Amount::from_sat(100_000_000).expect("valid amount");
println!("Large Amount = {}", large);

// Parsing string type to Amount - result handling needed for potential error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything that is code needs back ticks. Here Amount and a bunch of function names elsewhere.

@@ -0,0 +1,21 @@
Bitcoin is majorly expressed as BTC(bitcoins), mBTC(millibitcoins), sats(satoshis) or bits.
Copy link
Member

@tcharding tcharding Mar 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not keen on this line. Because of the wording: 'majorly express in'. But I rekon better to just mention sats and BTC, but that could be subjective.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "usually expressed in" is more idiomotic English.

BTW, these weird expressions make me think this is not LLM output. Though I agree that it often explains things that are too basic to be worth explaining.


The satoshi, named after the original bitcoin creator, Satoshi Nakamoto, is the smallest unit of bitcoin currency representing a hundred millionth of one Bitcoin (0.00000001 BTC).

The Bitcoin source code uses satoshi to specify any Bitcoin amount and all amounts on the blockchain are denominated in satoshi before they get converted for display.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we documenting the protocol or the rust-bitcoin lib, its not obvious to me from some of these docs which direction we are going in?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda liked these two sentences. Yes, they're "off-topic" in that they're about the protocol rather than this library, but they're useful context for why we have such a weird and large API for Amount.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll cut out some of the fluff

@Zeegaths Zeegaths closed this Mar 16, 2026
@apoelstra
Copy link
Member

Sigh. Guess you were right @tcharding

I wonder if it makes sense to move this repo to Forgejo....we hadn't really considered stopping hosting rust-bitcoin.org on Github. On the one hand, self-hosting a website is pretty-much the easiest thing we can do. On the other hand it's still kinda a PITA.

@tcharding
Copy link
Member

tcharding commented Mar 17, 2026

#44

I put an ernest 20 minutes at least into reviewing this yesterday for it just to be closed. I am getting close to not wanting to review PRs from external contributors, which is a bit sad really.

@Zeegaths Zeegaths reopened this Mar 18, 2026
@Zeegaths
Copy link
Author

#44

I put an ernest 20 minutes at least into reviewing this yesterday for it just to be closed. I am getting close to not wanting to review PRs from external contributors, which is a bit sad really.

Sorry for closing prematurely, I've addressed all the feedback.

@Zeegaths Zeegaths force-pushed the docs-amount-example branch from e1c8a0c to 273d971 Compare March 18, 2026 19:51
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 this pull request may close these issues.

3 participants