Automatically implement traits of enclosed type for Rust newtypes (tuple structs with one field)

is there a way to do it without extracting their “inner” values every time with pattern matching, and without implementing the Add, Sub, … traits and overloading operators? No, the only way is to implement the traits manually. Rust doesn’t have an equivalent to the Haskell’s GHC extension GeneralizedNewtypeDeriving which allows deriving on wrapper types … Read more

How to programmatically get the number of fields of a struct?

Are there any possible API like field_count() or is it only possible to get that via macros? There is no such built-in API that would allow you to get this information at runtime. Rust does not have runtime reflection (see this question for more information). But it is indeed possible via proc-macros! Note: proc-macros are … Read more

Go parse yaml file

If you’re working with google cloud or kubernetes more specifically and want to parse a service.yaml like this: apiVersion: v1 kind: Service metadata: name: myName namespace: default labels: router.deis.io/routable: “true” annotations: router.deis.io/domains: “” spec: type: NodePort selector: app: myName ports: – name: http port: 80 targetPort: 80 – name: https port: 443 targetPort: 443 Supplying … Read more

What lifetimes do I use to create Rust structs that reference each other cyclically?

It is not possible to create cyclic structures with borrowed pointers. There is not any good way of achieving cyclic data structures at present; the only real solutions are: Use reference counting with Rc<T> with a cyclic structure with Rc::new() and Rc:downgrade(). Read the rc module documentation and be careful to not create cyclic structures … Read more

Is it possible to declare local anonymous structs in Rust?

While anonymous structs aren’t supported, you can scope them locally, to do almost exactly as you’ve described in the C version: fn main() { struct Example<‘a> { name: &’a str }; let obj = Example { name: “Simon” }; let obj2 = Example { name: “ideasman42” }; println!(“{}”, obj.name); // Simon println!(“{}”, obj2.name); // ideasman42 … Read more

Sizeof struct in Go

Roger already showed how to use SizeOf method from the unsafe package. Make sure you read this before relying on the value returned by the function: The size does not include any memory possibly referenced by x. For instance, if x is a slice, Sizeof returns the size of the slice descriptor, not the size … Read more

nested struct initialization literals

While initialization the anonymous struct is only known under its type name (in your case A). The members and functions associated with the struct are only exported to the outside after the instance exists. You have to supply a valid instance of A to initialize MemberA: b := B { A: A{MemberA: “test1”}, MemberB: “test2”, … Read more

Struct’s zero value

Why guess (correctly) when there’s some documentation ? When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default … Read more

Initialize embedded struct in Go

req := new(MyRequest) req.PathParams = pathParams req.Request = origRequest or… req := &MyRequest{ PathParams: pathParams Request: origRequest } See: http://golang.org/ref/spec#Struct_types for more about embedding and how the fields get named.

Missing type in composite literal

The assignability rules are forgiving for anonymous types which leads to another possibility where you can retain the original definition of A while allowing short composite literals of that type to be written. If you really insist on an anonymous type for the B field, I would probably write something like: package main import “fmt” … Read more

tech