Are nested structs supported in Rust?
No, they are not supported. You should use separate struct declarations and regular fields: struct Foo {} struct Test { foo: Foo, }
No, they are not supported. You should use separate struct declarations and regular fields: struct Foo {} struct Test { foo: Foo, }
The error is on this line var s = Salutation The thing to the right of the = must evaluate to a value. Salutation is a type, not value. Here are three ways to declare s: var s Salutation // variable declaration using a type var s = Salutation{} // variable declaration using a value … Read more
There are a few problems. The first is that there’s nothing to require that an Animal also implements Clone. You could fix this by changing the trait definition: trait Animal: Clone { /* … */ } This would cause Animal to no longer be object safe, meaning that Box<dyn Animal> will become invalid, so that’s … Read more
In Go you don’t import types or functions, you import packages (see Spec: Import declarations). An example import declaration: import “container/list” And by importing a package you get access to all of its exported identifiers and you can refer to them as packagename.Identifiername, for example: var mylist *list.List = list.New() // Or simply: l := … Read more
Your Contact is a field with anonymous struct type. As such, you have to repeat the type definition: s := &Sender{ BankCode: “BC”, Name: “NAME”, Contact: struct { Name string Phone string }{ Name: “NAME”, Phone: “PHONE”, }, } But in most cases it’s better to define a separate type as rob74 proposed.
Commentary (and working) example: package main import “fmt” type Foo struct { name string } // SetName receives a pointer to Foo so it can modify it. func (f *Foo) SetName(name string) { f.name = name } // Name receives a copy of Foo since it doesn’t need to modify it. func (f Foo) Name() … Read more
To give a reference to OneOfOne’s answer, see the Conversions section of the spec. It states that A non-constant value x can be converted to type T in any of these cases: x is assignable to T. x‘s type and T have identical underlying types. x‘s type and T are unnamed pointer types and their … Read more
The mutability attribute is marked on a storage (constant or variable), not a type. You can think struct has two modes: mutable and immutable. If you assign a struct value to an immutable storage (we call it let or constant in Swift) the value becomes immutable mode, and you cannot change any state in the … Read more
It is definitely a neat trick. However, exposing pointers still makes direct access to data available, so it only buys you limited additional flexibility for future changes. Also, Go conventions do not require you to always put an abstraction in front of your data attributes. Taking those things together, I would tend towards one extreme … Read more
You can use == to compare with a zero value composite literal because all fields in Session are comparable: if (Session{}) == session { fmt.Println(“is zero value”) } playground example Because of a parsing ambiguity, parentheses are required around the composite literal in the if condition. The use of == above applies to structs where … Read more