How to join arrays like JavaScript's destructuring?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to join arrays like JavaScript's destructuring?



I want to make a function that receives a Vec and a position to change a number.


Vec



In JavaScript, this is really simple:


function replaceNumber(line, position, number)
return [
...line.slice(0, position),
number,
...line.slice(position+1, line.length)
]



How can I make a similar function in Rust?



I tried this:


fn replace_number(line: Vec<i32>, point: i32, number: i32) -> Vec<i32>
return [
&line[0..point as usize],
&[number],
&line[point as usize+1, point.len()]
].concat();



The result is an array of arrays. How can I make a destructuring like the JavaScript example?




1 Answer
1



I want to make a function that receives a Vec and a position to change a number.



Rust has indexing syntax, just like JavaScript, so there's really no need for the destructuring.


fn replace_number(mut line: Vec<i32>, point: usize, number: i32) -> Vec<i32>
line[point] = number;
line



or, more idiomatically:


fn replace_number(line: &mut Vec<i32>, point: usize, number: i32)
line[point] = number



Even more idiomatically would be to not have this function, probably, and just write it inline...



I want to know about immutably adding



With your original code, there's zero concern about anything "bad" happening because of mutability:


fn replace_number(line: Vec<i32>, point: i32, number: i32) -> Vec<i32>



This function takes ownership of the Vec, so the concept of immutability is rather moot here — the caller no longer has the Vec to care if it's mutated or not!


Vec


Vec



If you wanted to share data, you'd use a reference (a slice, specifically) like &[i32]. This is inherently immutable — you can't change it if you wanted to. You'd have to clone all the children and make the vector mutable:


&[i32]


fn replace_number(line: &[i32], point: usize, number: i32) -> Vec<i32>
let mut line = line.to_owned();
line[point] = number;
line



If you really wanted something like the JS syntax, you can use concat:


concat


fn replace_number(line: &[i32], point: usize, number: i32) -> Vec<i32>
[&line[..point], &[number], &line[point + 1..]].concat()



See also:





I want to know about immutability adding. I will change the function name. Is a good practice uses mutable variables in rust? I think that "is possible, but isnt good to use". I'm wrong?
– Renato Cassino
Aug 8 at 14:04





@RenatoCassino that's not an easy question to answer. What are your reasons for avoiding mutability in JS? Do you know why mutability causes problems? Rust solves many (but not all) of those problems in different ways.
– Shepmaster
Aug 8 at 14:11






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS