This is an example of the lengths to which pattern matching can be taken in Rust. There's an enum variant named Declaration_ which contains a value of type Declaration. The code you quoted is equivalent to the following:

Declaration_(decl) => {
  let l = decl.location;
  let n = decl.name;
  let v = decl.value;
  let i = decl.important;
}

You can find out more about all of these types in the public Servo documentation: http://doc.servo.org/cssparser/ast/struct.Declaration.html

The Rust guide also has a section on patterns that covers this usage: http://doc.rust-lang.org/guide.html#patterns

Cheers,
Josh

On 2014-10-24 8:19 PM, Gilles Leblanc wrote:
Hello,

I'm having some problems understanding the following line of code in the
servo codebase:
https://github.com/servo/servo/blob/master/components/style/properties/mod.rs.mako#L1524

Declaration_(Declaration{ location: l, name: n, value: v, important: i}) =>
{

The code is doing some pattern matching. `_` normally matches against every
other case. In this case it's preceded by Declaration which I understand as
if `item` matches the result of any function starting with Declaration and
with any suffix which takes a parameter of type Declaration which is
probably a struct in which we destructure the values localtion, name, value
and important.

I can't  seem to find where Declaration is defined, where `mportant` comes
from and I'm really not sure about my interpretation of the meaning of this
line of code.

Any pointers would be appreciated,
Thanks.

Here's the line in context:

match item {
             DeclAtRule(rule) => log_css_error(
                 rule.location, format!("Unsupported at-rule in
declaration list: @{:s}", rule.name).as_slice()),
             Declaration_(Declaration{ location: l, name: n, value: v,
important: i}) => {
                 // TODO: only keep the last valid declaration for a given name.
                 let (list, seen) = if i {
                     (&mut important_declarations, &mut important_seen)
                 } else {
                     (&mut normal_declarations, &mut normal_seen)
                 };


_______________________________________________
dev-servo mailing list
dev-servo@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-servo

Reply via email to