JavaScript (Node.js) and Rust, side by side
Number type: the range check is manual, and the last two lines show f64 rounding.function applyAdjustment(stock, delta) {
const next = stock + delta;
if (!Number.isInteger(next) || next < 0 || next > 65535) return null;
return next;
}
console.log(applyAdjustment(10, -3));
console.log(applyAdjustment(2, -5));
console.log(0.1 + 0.2 === 0.3); // every Number is an f64
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992, not ...993i32::from, add with checked_add, narrow with u16::try_from: every boundary is visible.fn apply_adjustment(stock: u16, delta: i32) -> Option<u16> {
let next = i32::from(stock).checked_add(delta)?;
u16::try_from(next).ok()
}
fn main() {
println!("{:?}", apply_adjustment(10, -3));
println!("{:?}", apply_adjustment(2, -5));
println!("{:?}", apply_adjustment(1, i32::MAX));
println!("{}", 0.1_f64 + 0.2 == 0.3);
println!("{:?} {}", 200_u8.checked_add(100), 200_u8.wrapping_add(100));
}| JavaScript (Node.js) | Rust |
|---|---|
stock + delta works for any two Numbers. | stock + delta with a u16 and an i32 is a type error; widen first with i32::from(stock). |
3 - 5 is -2. | u32 values 3 minus 5 panic in a debug build and wrap in release; 3_u32.checked_sub(5) is None. |
| Integers above 2^53 − 1 silently lose precision. | u64 and i64 are exact over their whole range; leaving it is an explicit overflow case. |
n | 0 truncates through int32. | as also truncates silently; prefer try_from when a value might not fit. |
Understand it
JavaScript has one Number, an IEEE 754 double. Integers are exact only up to Number.MAX_SAFE_INTEGER (2^53 − 1), 0.1 + 0.2 === 0.3 is false, and the bitwise operators quietly convert to 32-bit integers. BigInt exists but does not mix with Number. Rust has fixed-size integers from i8 to i128 and u8 to u128, the pointer-sized usize and isize, and f32 and f64. Floating point behaves as it does in JavaScript; integers are where your habits need to change.
Rust never converts between numeric types for you. u16 + i32 does not compile, even when every value would fit. Lossless widening is i32::from(stock) or .into(). Narrowing that may fail is u16::try_from(n), which returns a Result. as converts too, but it truncates or wraps silently, so keep it for casts you have proved safe.
Overflow is a decision, not an accident. In a debug build, u32 arithmetic that leaves the range panics with attempt to subtract with overflow; in a release build it wraps unless overflow-checks is enabled. Neither gives JavaScript's -2. When the boundary is part of the contract, say so: checked_sub returns None, saturating_sub stops at 0 and wrapping_sub wraps on purpose.
Choose types from the domain. Counts and lengths are usually usize or u32, money is an integer number of cents rather than an f64, and a value that can be negative needs a signed type. The lab's seats_left shows why the choice matters: a u32 has no negative answer, so the function returns Option<u32> and the caller handles the overbooked case.
The JavaScript (Node.js) habit, written in Rust
This is how the JavaScript (Node.js) version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.
fn main() {
let stock: u16 = 10;
let delta: i32 = -3;
let next = stock + delta;
println!("{next}");
}Try it
Port the JavaScript seatsLeft shown at the top of solution.rs. Return Some(left) when the capacity covers booked plus requested, otherwise None, and never panic or wrap, whatever u32 values arrive. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness passes every case, including exact capacity and bookings beyond u32::MAX. Printing a success message alone does not complete this lab.
Check your understanding
fn left(capacity: u32, booked: u32) -> u32 { capacity - booked } is called with 3 and 5 in a debug build. What happens?
Reveal the answer
- It panics with
attempt to subtract with overflow - It returns
-2, as in JavaScript - It returns
0, because unsigned values saturate
Debug builds check integer overflow and panic. A release build would wrap to 4294967294 unless overflow checks are enabled; use checked_sub when the boundary is expected.
Go deeper
Common misconception: “An unsigned subtraction below zero just gives a negative number, as in JavaScript.”
A u32 has no negative values. Leaving the range panics in debug builds and wraps in release builds unless overflow checks are on; checked_sub turns the boundary into None.
- Read the types:
stockis au16anddeltaani32. They cannot be added directly, soi32::from(stock)widens the stock without loss. - Follow each boundary:
checked_addreturnsNoneif the sum leavesi32, and?returns thatNoneearly.u16::try_from(next).ok()turns a negative or too-large result intoNone. - Compare with evidence: Run it:
Some(7),None,None,false, thenNone 44. The third call addsi32::MAXand would panic in a debug build withoutchecked_add.
Transfer challenge: Replace checked_add(delta)? with + delta and call apply_adjustment(1, i32::MAX) in a debug build. Rebuild with rustc -O and compare. Then try saturating_add and decide which behaviour fits a stock counter.
Connections
- const and let vs let and let mut: Immutable bindings, mut and shadowing
- JavaScript functions vs fn: Typed parameters and tail expressions

Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Number vs Rust integer types (courseId "rust-for-nodejs-developers", lessonId "node-number-to-integer-types") Link: https://crabrix.com/learn/rust-for-nodejs-developers/node-number-to-integer-types/ Goal: Numeric types never mix implicitly: convert with From or try_from, and choose checked, wrapping or saturating arithmetic where overflow is possible. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-nodejs-developers/node-number-to-integer-types) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.