> For the complete documentation index, see [llms.txt](https://til.duyet.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://til.duyet.net/programming/rust/imperative-vs-declarative.md).

# Imperative vs Declarative

**Declarative programs** we are describing **what** to do, rather than **how** to do it.

### Imperative

```rust
let mut sum = 0;
for i in 1..11 {
    sum += i;
}
println!("{}", sum);
```

### **Declarative**

### Imperative

```rust
let mut sum = 0;
for i in 1..11 {
    sum += i;
}
println!("{}", sum);
```

### **Declarative**

**Declarative programs** we are describing **what** to do, rather than **how** to do it.

```rust
println!("{}", (1..11).fold(0, |a, b| a + b));
```
