reference

If Python slice copy the reference, why can't I use it to modify the original list?

和自甴很熟 提交于 2020-06-17 02:14:26
问题 I know that Slicing lists does not generate copies of the objects in the list; it just copies the references to them. But if that's the case, then why doesn't this work? l = [1, 2, 3] # Attempting to modify the element at index 1 l[0:2][-1] = 10 # but the attempt fails. The original list is unchanged l > [1, 2, 3] Shouldn't l[0:2][-1] point to the element at index 1 of the original list? 回答1: You are right that slicing doesn't copy the items in the list. However, it does create a new list

If Python slice copy the reference, why can't I use it to modify the original list?

两盒软妹~` 提交于 2020-06-17 02:14:00
问题 I know that Slicing lists does not generate copies of the objects in the list; it just copies the references to them. But if that's the case, then why doesn't this work? l = [1, 2, 3] # Attempting to modify the element at index 1 l[0:2][-1] = 10 # but the attempt fails. The original list is unchanged l > [1, 2, 3] Shouldn't l[0:2][-1] point to the element at index 1 of the original list? 回答1: You are right that slicing doesn't copy the items in the list. However, it does create a new list

C++ references and pointers at the compiler level

会有一股神秘感。 提交于 2020-06-09 10:27:32
问题 I'm trying to learn how C++ compilers handle references and pointers, in preparation for a compiler class that I'm taking next semester. I'm specifically interested in how compilers handle references in C++. The standard specifies that a reference is an "alias," but I don't know exactly what that means at the compiler level. I have two theories: A non-reference variable has an entry in the symbol table. When a reference to that variable is created, the compiler simply creates another lexeme

What is an idiomatic way to collect an iterator of &T into a collection of Ts?

会有一股神秘感。 提交于 2020-06-07 06:38:24
问题 I need to collect an iterator over a slice of &str s into a collection of &str s. The problem is that the iterator yields &&str s. I tried to map from &word to word , and while it works, I don't know if it is considered good or if there are better options available. The problem: use std::collections::HashSet; fn main() { let words = &["hello", "world", "I'm", "a", "Rustacean!"]; let hashset = words.iter().collect::<HashSet<&str>>(); } Playground error[E0277]: a collection of type `std:

What is an idiomatic way to collect an iterator of &T into a collection of Ts?

泪湿孤枕 提交于 2020-06-07 06:37:26
问题 I need to collect an iterator over a slice of &str s into a collection of &str s. The problem is that the iterator yields &&str s. I tried to map from &word to word , and while it works, I don't know if it is considered good or if there are better options available. The problem: use std::collections::HashSet; fn main() { let words = &["hello", "world", "I'm", "a", "Rustacean!"]; let hashset = words.iter().collect::<HashSet<&str>>(); } Playground error[E0277]: a collection of type `std:

What happens when I call std::mem::drop with a reference instead of an owned value?

心已入冬 提交于 2020-05-29 06:14:40
问题 fn main() { let k = "fire"; drop(k); println!("{:?}", k); } Playground Why am I still able to use k after dropping it? Does drop not deref a reference automatically? If yes, then why? What does the implementation of Drop look like for &str ? 回答1: What happens when I call std::mem::drop with a reference The reference itself is dropped. a reference instead of an owned value A reference is a value. Why am I still able to use k after dropping it? Because immutable pointers implement Copy . You

How do intertwined scopes create a “data race”? [duplicate]

孤街醉人 提交于 2020-05-28 06:59:20
问题 This question already has answers here : Why does Rust disallow mutable aliasing? (3 answers) Closed 10 days ago . The Rust book talks about having multiple readers and multiple mutable references to an object as a data race situation that may lead to issues. For example, this code: fn main() { let mut x = 1; let r1 = &mut x; *r1 = 2; let r2 = &mut x; *r2 = 3; println!("{}", r1); println!("{}", r2); } will be rejected by Rust compiler because r1 and r2 scopes are intertwined. But what is

When to return a pointer, scalar and reference in C++?

▼魔方 西西 提交于 2020-05-20 11:12:12
问题 I'm moving from Java to C++ and am a bit confused of the language's flexibility. One point is that there are three ways to store objects: A pointer, a reference and a scalar (storing the object itself if I understand it correctly). I tend to use references where possible, because that is as close to Java as possible. In some cases, e.g. getters for derived attributes, this is not possible: MyType &MyClass::getSomeAttribute() { MyType t; return t; } This does not compile, because t exists only

When to return a pointer, scalar and reference in C++?

我与影子孤独终老i 提交于 2020-05-20 11:12:09
问题 I'm moving from Java to C++ and am a bit confused of the language's flexibility. One point is that there are three ways to store objects: A pointer, a reference and a scalar (storing the object itself if I understand it correctly). I tend to use references where possible, because that is as close to Java as possible. In some cases, e.g. getters for derived attributes, this is not possible: MyType &MyClass::getSomeAttribute() { MyType t; return t; } This does not compile, because t exists only

Can I modify the target of a pointer passed as parameter?

血红的双手。 提交于 2020-05-16 13:55:12
问题 Can a function change the target of a pointer passed as parameter so that the effect remains outside the function? void load(type *parameter) { delete parameter; parameter = new type("second"); } type *pointer = new type("first"); load(pointer); In this minimal example, will pointer point to the second allocate object? If not, how can I get this kind of behavior? Update: To clarify my intention, here is the code I would use if the parameter would be a normal type instead of a pointer. In this