[rust-dev] Passing arguments bu reference

Vladimir Matveev dpx.infinity at gmail.com
Sun Jun 1 01:43:44 PDT 2014


Aw, Gmail makes it so easy to press "Reply" instead of "Reply to all".
See below :)

> Hi, Christophe,
>
> Because `Vec` looks like this:
>
> struct Vec<T> {
>     len: uint,
>     cap: uint,
>     data: *mut T
> }
>
> its actual size is just three words, so you can freely pass it around
> regardless of number of items in it. So, the correct signature is this
> one:
>
> fn my_func(src: Vec<u8>) -> Vec<u8>
>
> However, it actually depends on your actual use case. Passing a vector
> by value means that the function you pass it to will consume it, and
> you won't be able to use it again in the calling code:
>
> let v = vec!(1, 2, 3);
> my_func(v);
> v.push(4);  // error: use of moved value `v`
>
> If you don't need this (and it happens very rarely, in fact), you
> should use references. Whether they are mutable or not depends on what
> you want to do with the vector.
>
> fn modify_vector_somehow(v: &mut Vec<u8>) { ... }
>
> let v = vec!(1, 2, 3);
> modify_vector_somehow(&mut v);
> v.push(4);  // all is fine
>
> 2014-06-01 12:34 GMT+04:00 Christophe Pedretti <christophe.pedretti at gmail.com>:
>> Hello all,
>>
>> I've read this :
>> http://words.steveklabnik.com/pointers-in-rust-a-guide
>>
>> I am coming from Java where everything is passed and returned by
>> reference (except for primitive data types), no choice.
>>
>> I know that with C, you have to use pointers to avoid passing and
>> returning by value.
>>
>> When i read the mentionned guide, things are not so evident with Rust.
>>
>> So, to be precise, imagine i need to write à fonction which takes à
>> big Vec<u8> (In my case it´s an SQL BLOB) as argument and returns a
>> big Vec<u8>
>>
>> Should i use
>> Fn my_func(src : &Vec<u8>) -> &Vec<u8>
>> Fn my_func(src : &Vec<u8>) -> ~Vec<u8>
>> Fn my_func(src : &Vec<u8>) ->Vec<u8>
>> Fn my_func(src : Vec<u8>) -> Vec<u8>
>> Fn my_func(src : ~Vec<u8>) -> ~Vec<u8>
>> Any other combination ?
>>
>> Thanks
>>
>> PS : i know that i have to use lifetimes and that ~ are now Box, i've
>> omitted them to simplify my demand
>> PS2 : genrally with a language you can accomplish the same thing with
>> different methods, but there are also common "usages", even if Rust is
>> young, what is the usage for passing and returning large data values
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev at mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev

2014-06-01 12:34 GMT+04:00 Christophe Pedretti <christophe.pedretti at gmail.com>:
> Hello all,
>
> I've read this :
> http://words.steveklabnik.com/pointers-in-rust-a-guide
>
> I am coming from Java where everything is passed and returned by
> reference (except for primitive data types), no choice.
>
> I know that with C, you have to use pointers to avoid passing and
> returning by value.
>
> When i read the mentionned guide, things are not so evident with Rust.
>
> So, to be precise, imagine i need to write à fonction which takes à
> big Vec<u8> (In my case it´s an SQL BLOB) as argument and returns a
> big Vec<u8>
>
> Should i use
> Fn my_func(src : &Vec<u8>) -> &Vec<u8>
> Fn my_func(src : &Vec<u8>) -> ~Vec<u8>
> Fn my_func(src : &Vec<u8>) ->Vec<u8>
> Fn my_func(src : Vec<u8>) -> Vec<u8>
> Fn my_func(src : ~Vec<u8>) -> ~Vec<u8>
> Any other combination ?
>
> Thanks
>
> PS : i know that i have to use lifetimes and that ~ are now Box, i've
> omitted them to simplify my demand
> PS2 : genrally with a language you can accomplish the same thing with
> different methods, but there are also common "usages", even if Rust is
> young, what is the usage for passing and returning large data values
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev


More information about the Rust-dev mailing list