# Tch-rs Allows a tensor that is not mutable to change

**URL:** <https://users.rust-lang.org/t/tch-rs-allows-a-tensor-that-is-not-mutable-to-change/140725>\
**Category:** help\
**Created:** [June 15, 2026, 12:21pm UTC](https://users.rust-lang.org/t/tch-rs-allows-a-tensor-that-is-not-mutable-to-change/140725 "2026-06-15T12:21:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradbell](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@bradbell](https://users.rust-lang.org/u/bradbell)\
**Post date:** [June 15, 2026, 12:21pm UTC](https://users.rust-lang.org/t/tch-rs-allows-a-tensor-that-is-not-mutable-to-change/140725/1 "2026-06-15T12:21:46Z")

</div>

Why does tch-rs allow a tensor that is not mutable to change ? Here is a simple example:

Cargo.toml

```rust
[package]
name = "use-tch-rs"
version = "0.1.0"
edition = "2024"

[dependencies]
tch = { version = "0.24.0", features = ["download-libtorch"] }

```

src/main.rs

```rust
fn main() {
    use tch::Tensor;
    //
    let tensor = Tensor::zeros(&[2, 2], (tch::Kind::Float, tch::Device::Cpu));
    println!( "{}", tensor );
    let mut element = tensor.get(0).get(0); 
    let _ = element.fill_(6.0);
    println!( "{}", tensor );
}

```

cargo run

```rust
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/use-tch-rs`
[[0., 0.],
 [0., 0.]]
Tensor[[2, 2], Float]
[[6., 0.],
 [0., 0.]]
Tensor[[2, 2], Float]

```

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [June 15, 2026, 12:46pm UTC](https://users.rust-lang.org/t/tch-rs-allows-a-tensor-that-is-not-mutable-to-change/140725/2 "2026-06-15T12:46:38Z")

</div>

`tch` is a crate wrapping the libtorch C++ library. I'm no FFI expert, but I assume this is fine as Rust's aliasing and mutability guarantees do not extend into C++ memory.

---

<div class="post-metadata">

**Author:** ![bradbell](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@bradbell](https://users.rust-lang.org/u/bradbell)\
**Post date:** [June 15, 2026, 1:12pm UTC](https://users.rust-lang.org/t/tch-rs-allows-a-tensor-that-is-not-mutable-to-change/140725/3 "2026-06-15T13:12:05Z")

</div>

I was hoping that tch-rs would make const and mutable (and hence parallel programming) simpler. Here is a similar example in C++ libtorch:

main.cpp

```rust
#include <torch/torch.h>
int main() {
    const at::Tensor tensor = torch::zeros({2,2});
    std::cout << "tensor =\n" << tensor << std::endl;
    at::Tensor other = tensor;
    other [0][0] = 5.0;
    std::cout << "tensor =\n" << tensor << std::endl;
    return 0;
}

```

Output

```rust
tensor =
 0 0
 0 0
[CPUFloatType{2,2}]
tensor =
 5 0
 0 0
[CPUFloatType{2,2}]

```

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [June 15, 2026, 3:16pm UTC](https://users.rust-lang.org/t/tch-rs-allows-a-tensor-that-is-not-mutable-to-change/140725/4 "2026-06-15T15:16:33Z")

</div>

> [@bradbell](#):
>
> I was hoping that tch-rs would make const and mutable (and hence parallel programming) simpler.

there's a choice for the api design, and apparently the author of `tch-rs` chose to mimic the C++ api. quoting the very first paragraph of the readme file:

> The goal of the `tch` crate is to provide some thin wrappers around the C++ PyTorch api (a.k.a. libtorch). It aims at staying as close as possible to the original C++ api. More idiomatic rust bindings could then be developed on top of this.

if you don't like the api design of this `libtorch` _binding_ crate, an alternative to `tch` is `candle`, which should feel more idiomatic.

note, the implementation of `candle` still has reference semantics and interior mutability, it uses internal locks (an `Arc<RwLock<...>>`, to be specific) , similar to `libtorch`, but the api is mostly value based. it actually takes non trivial effort to do in-place modifications.

> **[Tensor in candle\_core - Rust](https://docs.rs/candle-core/latest/candle_core/struct.Tensor.html)**
>
> The core struct for manipulating tensors.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 13, 2026, 3:17pm UTC](https://users.rust-lang.org/t/tch-rs-allows-a-tensor-that-is-not-mutable-to-change/140725/5 "2026-09-13T15:17:29Z")

</div>

Closed on September 13, 2026, 3:17pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
