destructuring

What is the purpose of `&` before the loop variable?

点点圈 提交于 2020-02-11 08:40:21
问题 What is the purpose of & in the code &i in list ? If I remove the & , it produces an error in largest = i , since they have mismatched types (where i is &32 and i is i32 ). But how does &i convert i into i32 ? fn largest(list: &[i32]) -> i32 { println!("{:?}", list); let mut largest = list[0]; for &i in list { if i > largest { largest = i; } } largest } fn main() { let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32]; println!("The largest number is: {}", largest(&hey)); } Playground It

What is the purpose of `&` before the loop variable?

谁都会走 提交于 2020-02-11 08:40:20
问题 What is the purpose of & in the code &i in list ? If I remove the & , it produces an error in largest = i , since they have mismatched types (where i is &32 and i is i32 ). But how does &i convert i into i32 ? fn largest(list: &[i32]) -> i32 { println!("{:?}", list); let mut largest = list[0]; for &i in list { if i > largest { largest = i; } } largest } fn main() { let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32]; println!("The largest number is: {}", largest(&hey)); } Playground It

What is the purpose of `&` before the loop variable?

眉间皱痕 提交于 2020-02-11 08:39:08
问题 What is the purpose of & in the code &i in list ? If I remove the & , it produces an error in largest = i , since they have mismatched types (where i is &32 and i is i32 ). But how does &i convert i into i32 ? fn largest(list: &[i32]) -> i32 { println!("{:?}", list); let mut largest = list[0]; for &i in list { if i > largest { largest = i; } } largest } fn main() { let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32]; println!("The largest number is: {}", largest(&hey)); } Playground It

How to destructure an object based on a dynamic defined variable

◇◆丶佛笑我妖孽 提交于 2020-02-02 06:50:11
问题 I am working with an immutable object that I need to add subtract an array of values from. I know it's possible using ES6 destructing with the following. const {countries, remainder} = someObj // {countries:...,languages:..., ...keys}; This will end me up with the remainder being the new Object without the countries key. Therefore I figured out that I could use reduce on an array to go through and remove all the values from the object, returning a new object in the end, using the default

destructuring assignment default value [duplicate]

巧了我就是萌 提交于 2020-01-31 05:01:40
问题 This question already has an answer here : destructuring falsey and null with default parameters (1 answer) Closed 2 years ago . I am learning javascript and I got kind of stuck with ES6 syntax while trying to give a default value to a variable when destructuring. Basically, I am trying to assign a variable giving the value of an object's property to it and if the value is false/null/undefined, I want it to be an empty object. For example, let foo = { prop1: 'hello!', prop2: null } const

Destructuring and rename property

人走茶凉 提交于 2020-01-29 14:10:38
问题 const a = { b: { c: 'Hi!' } }; const { b: { c } } = a; Is it possible rename b in this case? I want get c and also rename b . 回答1: You could destructure with a renaming and take the same property for destructuring. const a = { b: { c: 'Hi!' } }; const { b: formerB, b: { c } } = a; console.log(formerB) console.log(c); 回答2: You can destructure the same property multiple times, onto different targets: const { b: {c}, b: d } = a; This assigns a.b.c to c and a.b to d . 来源: https://stackoverflow

Destructuring and rename property

时光总嘲笑我的痴心妄想 提交于 2020-01-29 14:09:56
问题 const a = { b: { c: 'Hi!' } }; const { b: { c } } = a; Is it possible rename b in this case? I want get c and also rename b . 回答1: You could destructure with a renaming and take the same property for destructuring. const a = { b: { c: 'Hi!' } }; const { b: formerB, b: { c } } = a; console.log(formerB) console.log(c); 回答2: You can destructure the same property multiple times, onto different targets: const { b: {c}, b: d } = a; This assigns a.b.c to c and a.b to d . 来源: https://stackoverflow

Destructuring and rename property

陌路散爱 提交于 2020-01-29 14:08:36
问题 const a = { b: { c: 'Hi!' } }; const { b: { c } } = a; Is it possible rename b in this case? I want get c and also rename b . 回答1: You could destructure with a renaming and take the same property for destructuring. const a = { b: { c: 'Hi!' } }; const { b: formerB, b: { c } } = a; console.log(formerB) console.log(c); 回答2: You can destructure the same property multiple times, onto different targets: const { b: {c}, b: d } = a; This assigns a.b.c to c and a.b to d . 来源: https://stackoverflow

Initialization of val by destructuring in Kotlin

半城伤御伤魂 提交于 2020-01-21 19:01:44
问题 Initially I wanted to achieve class NotationDiceRoll(notation: String) { val rolls: Int val sides: Int init { parseNotation(notation) } private fun parseNotation(notation: String) { rolls = 1 sides = 4 } } But Kotlin complains that "Val cannot be reassigned". It seems that the only place where the vals can be assigned is the init block. Alright then, it is more obvious after all. So I changed it to class NotationDiceRoll(notation: String) { val rolls: Int val sides: Int init { (rolls, sides)

ES6 destructuring function parameter - naming root object

≡放荡痞女 提交于 2020-01-09 11:39:29
问题 Is there a way to retain the name of a destructured function argument? I.e., the name of the root object? In ES5, I might do this (using inheritance as a metaphor to make the point): // ES5: var setupParentClass5 = function(options) { textEditor.setup(options.rows, options.columns); }; var setupChildClass5 = function(options) { rangeSlider.setup(options.minVal, options.maxVal); setupParentClass5(options); // <= we pass the options object UP }; I'm using the same options object to hold