Skip to main content

List

Trait List 

Source
pub trait List {
    // Required methods
    fn len(&self) -> usize;
    fn get(&self, index: usize) -> Value<'_>;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

A sequence a template can loop over.

use web_net::{List, Value};

struct Tags<'a>(&'a [&'a str]);
impl List for Tags<'_> {
    fn len(&self) -> usize { self.0.len() }
    fn get(&self, index: usize) -> Value<'_> {
        self.0.get(index).map_or(Value::Absent, |t| Value::Text(t))
    }
}

let tags = Tags(&["rust", "web"]);
assert_eq!(tags.get(1).as_text(), Some("web"));
assert!(tags.get(2).is_absent());
assert!(!tags.is_empty());

Required Methods§

Source

fn len(&self) -> usize

The number of items.

Source

fn get(&self, index: usize) -> Value<'_>

An item, or Value::Absent past the end.

Provided Methods§

Source

fn is_empty(&self) -> bool

Whether the list is empty.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§