reference

Understanding of reference life time in Rust

我是研究僧i 提交于 2021-01-29 00:34:54
问题 I'm a new Rust user and I'm reading a book The Complete Rust Programming Reference Guide . In the book there is an example: fn main() { let mut a = String::from("testing"); let a_ref = &mut a; a_ref.push('!'); println!("{}", a); } The book states the code will generate an error. However, on my local machine, I can run it without any issue. Is this because I'm using a newer Rust compiler [ rustc 1.41.0-nightly (412f43ac5 2019-11-24) ] and the code doesn't work on older ones? I've read some

Python: Redefine function so that it references its own self

与世无争的帅哥 提交于 2021-01-28 09:47:38
问题 Say I have got some function fun , the actual code body of which is out of my control. I can create a new function which does some preprocessing before calling fun , i.e. def process(x): x += 1 return fun(x) If I now want process to take the place of fun for all future calls to fun , I need to do something like # Does not work fun = process This does not work however, as this creates a cyclic reference problem as now fun is called from within the body of fun . One solution I have found is to

Understanding for loop semantics when iterating through a vector containing mutable references

元气小坏坏 提交于 2021-01-28 08:16:42
问题 I am trying to understand why the following code fails: fn main() { let mut a = 10; let mut b = 20; let mut c = 30; let p = vec![&mut a, &mut b, &mut c]; // works with [&a, &b, &c] for &x in &p { // works with 'x' instead of of '&x' println!("{}", x); } } The error message is: error[E0507]: cannot move out of borrowed content --> src/main.rs:7:9 | 7 | for &x in &p { | ^- | || | |hint: to prevent move, use `ref x` or `ref mut x` | cannot move out of borrowed content As I understand, the

Unable to lookup resource on component level in Websphere

烂漫一生 提交于 2021-01-28 07:32:01
问题 At the moment I'm working on an application that has to use a connection factory. When I lookup the connection factory directly on a global level by the name set in WAS everything is working fine, but for means of decoupling I want to define a resource reference in my application and lookup that name. So I created following entry in my application.xml: <resource-ref> <res-ref-name>jms/connectionFactory</res-ref-name> <res-type>javax.jms.ConnectionFactory</res-type> <res-auth>Container</res

Force C++ to assign new addresses to arguments

≡放荡痞女 提交于 2021-01-28 06:10:43
问题 It seems that when I pass different integers directly to a function, C++ assigns them the same address as opposed to assigning different addresses to different values. Is this by design, or an optimization that can be turned off? See the code below for an illustration. #include <iostream> const int *funct(const int &x) { return &x; } int main() { int a = 3, b = 4; // different addresses std::cout << funct(a) << std::endl; std::cout << funct(b) << std::endl; // same address std::cout << funct

undefined reference when including a header file

帅比萌擦擦* 提交于 2021-01-28 05:17:07
问题 I get an error when I include a header file, but not if I include the source file instead. The function is defined in the source file like this: /* in User.c */ struct User { const char* name; }; struct User* addedUser(const char* name) { struct User* user = malloc(sizeof(struct User)); user->name = name; return user; } And used like this: /* in main.c */ int test_addedUser() { char* newName = "Fooface"; struct User* newUser = addedUser(newName); assert(!strcmp(newName, newUser->name));

Apply required field to referenced JSON data schema

无人久伴 提交于 2021-01-28 00:54:20
问题 I have the following use-case I try to solve with JSON schemas. I have a generic JSON data schema for, for example, a user. Here is an example of the user.schema.json file. { "type": "object", "definitions": {}, "$schema": "http://json-schema.org/draft-07/schema#", "properties": { "name": { "type": "string", "minLength": 1 }, "email": { "type": "string", "minLength": 1 }, "locale": { "type": "string", "minLength": 1 }, "active": { "type": "boolean", "default": true }, "password": { "type":

What exactly is invalidation of reference/pointer?

本小妞迷上赌 提交于 2021-01-27 05:28:41
问题 I cannot find any definition for invalidation of pointers/references in the Standard. I ask because I just found out that C++11 forbids copy-on-write (COW) for strings. As far as I understand, if COW was applied then p would be still a valid pointer and r a valid reference after the following commands: std::string s("abc"); std::string s2(s); char * p = &(s2[0]); char & r = s2[0]; s2[1] = "B"; Just they would no longer point/refer to the first character of s2 , but merely to the first

Go: reference types as arguments

心已入冬 提交于 2021-01-22 06:31:15
问题 Certain types in Go are reference types: maps, slices, channels, functions, and methods. Sometimes you need to use pointers to references. For example, type Stack []interface{} func (stack *Stack) Push(x interface{}) { *stack = append(*stack, x) } You need it because all arguments are passed by copying the value, and append() might need to reallocate memory in the slice's capacity is not big enough. I get that. First question. How about map types? If I have a custom type based on a map ,

Go: reference types as arguments

*爱你&永不变心* 提交于 2021-01-22 06:29:08
问题 Certain types in Go are reference types: maps, slices, channels, functions, and methods. Sometimes you need to use pointers to references. For example, type Stack []interface{} func (stack *Stack) Push(x interface{}) { *stack = append(*stack, x) } You need it because all arguments are passed by copying the value, and append() might need to reallocate memory in the slice's capacity is not big enough. I get that. First question. How about map types? If I have a custom type based on a map ,