577 words
3 minutes
clamp_basic002
2026-08-18

link#


clamp#

The basic form is:

value.clamp(min, max)

It means:

if value < min  → min
if value > max  → max
otherwise       → value

1. Basic integer example#

fn main() {
    let x = 75;

    let result = x.clamp(0, 100);

    println!("{result}");
}

Output:

75

2. Value below the minimum#

fn main() {
    let x = -10;

    let result = x.clamp(0, 100);

    println!("{result}");
}

Output:

0

Visual:

             clamp(0, 100)
                  │
                  ▼
-10 ──────────────┬──────────────────────────────
                  0             100
                  ▲
                  │
               result

3. Value above the maximum#

fn main() {
    let x = 150;

    let result = x.clamp(0, 100);

    println!("{result}");
}

Output:

100

So:

x = 150

150
 │
 ▼
clamp(0, 100)
 │
 ▼
100

4. 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:

1

This 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:

0

This guarantees:

0.0 <= volume <= 1.0

6. 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 <= 4

7. 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 + offset

becomes:

50 + 5
  │
  ▼
55

Then:

55.clamp(0, 51)

becomes:

51

Then:

as usize

becomes:

51_usize

So 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                          maximum

8. 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 = 51

Try 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 = 0

So:

QP + offset
       │
       ▼
  ┌─────────────┐
  │   clamp     │
  │   0 ..= 51  │
  └─────────────┘
       │
       ▼
valid QP

9. 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:

100

This is useful for things like:

health
energy
volume
brightness
opacity

10. 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:

20

You 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 ─────┴────────────┴───── max

Or 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.

clamp_basic002
https://younghakim7.github.io/blog/posts/clamp_basic002/
Author
YoungHa
Published at
2026-08-18