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

⚓ Rust    📅 2026-06-15    👤 surdeus    👁️ 2      

surdeus

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

Cargo.toml

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

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

src/main.rs

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

    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]

3 posts - 2 participants

Read full topic

🏷️ Rust_feed