link
clamp
- Absolutely. In Rust,
clampis useful when you want to force a value into a valid range.
The basic form is:
value.clamp(min, max)It means:
if value < min → min
if value > max → max
otherwise → value1. Basic integer example
fn main() {
let x = 75;
let result = x.clamp(0, 100);
println!("{result}");
}Output:
752. Value below the minimum
fn main() {
let x = -10;
let result = x.clamp(0, 100);
println!("{result}");
}Output:
0Visual:
clamp(0, 100)
│
▼
-10 ──────────────┬──────────────────────────────
0 100
▲
│
result3. Value above the maximum
fn main() {
let x = 150;
let result = x.clamp(0, 100);
println!("{result}");
}Output:
100So:
x = 150
150
│
▼
clamp(0, 100)
│
▼
1004. Clamping a floating-point value
clamp works with floating-point numbers too.
fn main() {
let brightness = 1.5_f32;
let brightness = brightness.clamp(0.0, 1.0);
println!("{brightness}");
}Output:
1This is common for graphics:
let r = r.clamp(0.0, 1.0);
let g = g.clamp(0.0, 1.0);
let b = b.clamp(0.0, 1.0);5. Clamping a negative value
fn main() {
let volume = -0.5_f32;
let volume = volume.clamp(0.0, 1.0);
println!("{volume}");
}Output:
0This guarantees:
0.0 <= volume <= 1.06. Using clamp before indexing an array
This is very similar to your H.264 code.
fn main() {
let table = [10, 20, 30, 40, 50];
let qp = 100;
let index = qp.clamp(0, 4) as usize;
println!("{}", table[index]);
}Without clamp:
table[qp as usize]could attempt:
table[100]which is out of bounds.
With:
qp.clamp(0, 4)we guarantee:
0 <= index <= 47. Your H.264 example
Your code:
self.chroma_qp[0] =
CHROMA_QP8[(qp + off[0]).clamp(0, 51) as usize] as i32;Let’s break it down.
Suppose:
let qp = 50;
let offset = 5;First:
qp + offsetbecomes:
50 + 5
│
▼
55Then:
55.clamp(0, 51)becomes:
51Then:
as usizebecomes:
51_usizeSo the entire expression:
CHROMA_QP8[(qp + offset).clamp(0, 51) as usize]is effectively:
CHROMA_QP8[51]The purpose is to guarantee that the array index stays within:
0 ─────────────────────────────── 51
▲ ▲
minimum maximum8. Clamping after adding an offset
This is probably the most important pattern for your current code.
fn main() {
let qp = 48;
let offset = 10;
let adjusted_qp = (qp + offset).clamp(0, 51);
println!("QP = {adjusted_qp}");
}Output:
QP = 51Try a negative offset:
fn main() {
let qp = 3;
let offset = -10;
let adjusted_qp = (qp + offset).clamp(0, 51);
println!("QP = {adjusted_qp}");
}Output:
QP = 0So:
QP + offset
│
▼
┌─────────────┐
│ clamp │
│ 0 ..= 51 │
└─────────────┘
│
▼
valid QP9. Clamping a struct field
You can also modify a value directly:
struct Player {
health: i32,
}
fn main() {
let mut player = Player { health: 150 };
player.health = player.health.clamp(0, 100);
println!("{}", player.health);
}Output:
100This is useful for things like:
health
energy
volume
brightness
opacity10. clamp with min and max variables
The bounds don’t have to be literals.
fn main() {
let min = 10;
let max = 20;
let value = 35;
let result = value.clamp(min, max);
println!("{result}");
}Output:
20You can think of it as:
let result = value.clamp(min, max);being conceptually equivalent to:
let result = if value < min {
min
} else if value > max {
max
} else {
value
};That’s the key idea.
A useful mental model
Remember:
x.clamp(min, max)as:
min max
│ │
▼ ▼
───────────────●────────────●───────────────
│ │ │ │
│ │ │ │
x<min │ valid │ x>max
│ │ range │ │
▼ │ │ ▼
min ─────┴────────────┴───── maxOr simply:
-100.clamp(0, 10) == 0
5.clamp(0, 10) == 5
100.clamp(0, 10) == 10- One important detail: the bounds must satisfy
min <= max. For example:
10.clamp(20, 5)is invalid because the minimum is greater than the maximum.
For your H.264 code,
clamp(0, 51)is particularly appropriate because you’re explicitly restricting the computed QP to the valid 0–51 range before using it as an array index.