Skip to main content

Suggestion

Enum Suggestion 

Source
pub enum Suggestion {
    PlacePrediction(PlacePrediction),
    QueryPrediction(QueryPrediction),
}
Expand description

An Autocomplete suggestion result.

Represents a single suggestion from the autocomplete API, which can be either a specific place (like “Pizza Hut, 123 Main St”) or a general query suggestion (like “pizza restaurants near me”). The API may return a mix of both types, allowing users to either navigate to a specific location or explore a broader search query.

§Examples

match suggestion {
    Suggestion::PlacePrediction(place) => {
        println!("Place: {}", place.text().text());
        // Use place.place_id() for Place Details lookup
    }
    Suggestion::QueryPrediction(query) => {
        println!("Query: {}", query.query_string());
        // Use query text for search endpoint
    }
}

// Format with HTML regardless of type
let html = match &suggestion {
    Suggestion::PlacePrediction(p) => p.to_html("strong"),
    Suggestion::QueryPrediction(q) => q.to_html("strong"),
};

Variants§

§

PlacePrediction(PlacePrediction)

A prediction for a specific Place.

Represents a concrete location or establishment that the user can navigate to or get details about. Place predictions include identifiers that can be used with other Google Maps APIs.

§

QueryPrediction(QueryPrediction)

A prediction for a general query.

Represents a search text suggestion that can be used in search endpoints to find relevant places. Query predictions are exploratory and don’t represent specific locations.

Implementations§

Source§

impl Suggestion

Source

pub const fn is_place(&self) -> bool

Checks if this suggestion is a place prediction.

Returns true if the suggestion represents a specific place rather than a general query.

Source

pub const fn is_query(&self) -> bool

Checks if this suggestion is a query prediction.

Returns true if the suggestion represents a general search query rather than a specific place.

Source

pub const fn as_place(&self) -> Option<&PlacePrediction>

Returns a reference to the place prediction if this is a place.

Use this to access place-specific information like place IDs, types, and distance measurements. Returns None if this is a query prediction.

Source

pub const fn as_query(&self) -> Option<&QueryPrediction>

Returns a reference to the query prediction if this is a query.

Use this to access query text for search operations. Returns None if this is a place prediction.

Source

pub fn text(&self) -> &str

Returns the display text for this suggestion.

Extracts the text content regardless of whether this is a place or query prediction. Use this when you need the text without caring about the suggestion type.

Source

pub fn to_html(&self, tag: &str) -> String

Formats the suggestion text with HTML highlighting.

Applies HTML tags to matched portions regardless of suggestion type. Use this for simple HTML formatting without needing to match on the enum variant.

§Examples
let html = suggestion.to_html("mark");
// "<mark>Pizza</mark> Hut" (for either place or query)
Source

pub fn to_html_structured(&self, main_tag: &str, secondary_tag: &str) -> String

Formats the suggestion with structured HTML if available.

Applies different HTML tags to main and secondary text portions if structured format is available. Otherwise, falls back to highlighting the full text with the main tag. Works for both place and query predictions.

§Examples
let html = suggestion.to_html_structured("strong", "em");
// "<strong>Pizza</strong> Hut, <em>123 Main</em> St"
Source

pub fn place_id(&self) -> Option<&str>

Returns the Place ID associated with the suggestion, if any.

  • If the suggestion is a a place prediction this will return Some
  • If it’s a query prediction it will return None
Source

pub fn format_with<F>(&self, formatter: F) -> String
where F: FnMut(&str, bool) -> String,

Formats the suggestion with a custom formatter function.

Applies a custom formatting function to the text, distinguishing between matched and unmatched portions. Works for both place and query predictions, providing maximum flexibility for any output format (ANSI, Markdown, etc.).

§Examples

ANSI terminal colors:

let formatted = suggestion.format_with(|text, is_matched| {
    if is_matched {
        format!("\x1b[1;32m{}\x1b[0m", text)  // Bold green
    } else {
        text.to_string()
    }
});

Markdown:

let formatted = suggestion.format_with(|text, is_matched| {
    if is_matched {
        format!("**{}**", text)
    } else {
        text.to_string()
    }
});
Source

pub fn format_with_structured<F, G>( &self, main_formatter: F, secondary_formatter: G, ) -> String
where F: FnMut(&str, bool) -> String, G: FnMut(&str, bool) -> String,

Formats with custom formatters using structured format if available.

Applies different formatting functions to main and secondary text if structured format exists. Works for both place and query predictions. Falls back to the main formatter only if structured format is not available.

§Examples

ANSI terminal colors:

let formatted = suggestion.format_with_structured(
    |text, is_matched| {
        if is_matched {
            format!("\x1b[1;33m{}\x1b[0m", text)  // Bold yellow
        } else {
            text.to_string()
        }
    },
    |text, is_matched| {
        if is_matched {
            format!("\x1b[36m{}\x1b[0m", text)  // Cyan
        } else {
            text.to_string()
        }
    }
);

Trait Implementations§

Source§

impl Clone for Suggestion

Source§

fn clone(&self) -> Suggestion

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Suggestion

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Suggestion

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Suggestion

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats as the plain suggestion text.

Returns the text content without formatting, regardless of whether this is a place or query prediction.

Source§

impl From<PlacePrediction> for Suggestion

Source§

fn from(place: PlacePrediction) -> Self

Converts a PlacePrediction to a Suggestion.

Wraps a place prediction in the suggestion enum. Use this for convenience when constructing suggestion lists.

Source§

impl From<QueryPrediction> for Suggestion

Source§

fn from(query: QueryPrediction) -> Self

Converts a QueryPrediction to a Suggestion.

Wraps a query prediction in the suggestion enum. Use this for convenience when constructing suggestion lists.

Source§

impl PartialEq for Suggestion

Source§

fn eq(&self, other: &Suggestion) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Suggestion

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Suggestion

Source§

impl StructuralPartialEq for Suggestion

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,