linebender / linebender/druid

Error in Scope documentation example

Open
#2,139 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
9.7k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

Whilst I don't completely grasp Scopes, I believe the example given in the docs has a small mistake that makes it fundamentally incorrect.

The example as it stands:
```rust
use druid::{Data, Lens, WidgetExt};
use druid::widget::{TextBox, Scope};
#[derive(Clone, Data, Lens)]
struct AppState {
name: String,
}

#[derive(Clone, Data, Lens)]
struct PrivateState {
text: String,
other: u32,
}

impl PrivateState {
pub fn new(text: String) -> Self {
PrivateState { text, other: 0 }
}
}

fn main() {
let scope = Scope::from_lens(
PrivateState::new,
PrivateState::text,
TextBox::new().lens(PrivateState::text),
);
}
```

I believe the fourth to last line uses the wrong lens - the example is attempting to demonstrate the use of a lens on _the application state_ as a means of transferring start state into the private state. As it is, it just looks like circular references of private states initialised by other private states.

I believe the main function should instead look like this:
```rust
fn main() {
let scope = Scope::from_lens(
PrivateState::new,
AppState::name,
TextBox::new().lens(PrivateState::text),
);
}
```

Contributor guide

Open the contributing guide

Research direction

Review the Scope documentation example and verify how its constructor and lens arguments relate the application and private state. Check the example with the relevant Rust documentation or build process; it is done when the corrected example uses the application-state lens and remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.