d-unsed / ruru

Native Ruby extensions written in Rust
MIT License
832 stars 40 forks source link

Moving vs borrowing #60

Open d-unsed opened 7 years ago

d-unsed commented 7 years ago

Consider the following example:

let string = RString::new("Some string");
let mut array = Array::new();

// Moves `string` into `array` similarly to `vec.push()` in Rust
array.push(string);

// ... `string` cannot be used, because it's moved

vs

string = 'Some string'
array = []

# Takes a reference of the `string`
array.push(string)

# `string` can be used
string.concat(" mutated")

array.inspect # => ['Some string mutated']

It needs to be decided whether to take objects references (Ruby way) or by values (Rust way).

danielpclark commented 6 years ago

I think having the Rust way is preferable. We can manage the context of ownership like we normally would in Rust. We can let each language behave in their own way and this won't lead to surprises. I like being able to define the ownership in my own programs.

Ruby internally has most objects act as CoW.