This Rust library provides support for lenses, which are a mechanism in functional programming for focusing on a part of a complex data structure.
Add a dependency (or two) to your Cargo.toml
:
toml
[dependencies]
pl-lens = "1.0"
Then, in your crate:
rust
use pl_lens::*;
A Lens
can be used to transform a conceptually-immutable data structure by changing only a portion of the data. Let's demonstrate with an example:
```rust
struct Address { street: String, city: String, postcode: String }
struct Person { name: String, age: u8, address: Address }
let p0 = Person { name: "Pop Zeus".tostring(), age: 58, address: Address { street: "123 Needmore Rd".tostring(), city: "Dayton".tostring(), postcode: "99999".tostring() } }; asserteq!(lens!(Person.name).getref(&p0), "Pop Zeus"); asserteq!(lens!(Person.address.street).getref(&p0), "123 Needmore Rd");
let p1 = lens!(Person.address.street).set(p0, "666 Titus Ave".tostring()); asserteq!(lens!(Person.name).getref(&p1), "Pop Zeus"); asserteq!(lens!(Person.address.street).get_ref(&p1), "666 Titus Ave"); ```
pl-lens
is distributed under an MIT license. See LICENSE for more details.