Struct Expr

Source
pub struct Expr { /* private fields */ }
Expand description

Wolfram Language expression.

§Example

Construct the expression {1, 2, 3}:

use wolfram_expr::{Expr, Symbol};

let expr = Expr::normal(Symbol::new("System`List"), vec![
    Expr::from(1),
    Expr::from(2),
    Expr::from(3)
]);

§Reference counting

Internally, Expr is an atomically reference-counted ExprKind. This makes cloning an expression computationally inexpensive.

Implementations§

Source§

impl Expr

Source

pub fn try_as_normal(&self) -> Option<&Normal>

If this is a Normal expression, return that. Otherwise return None.

Source

pub fn try_as_bool(&self) -> Option<bool>

If this is a True or False symbol, return that. Otherwise return None.

Source

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

If this is a ExprKind::String expression, return that. Otherwise return None.

Source

pub fn try_as_symbol(&self) -> Option<&Symbol>

If this is a Symbol expression, return that. Otherwise return None.

Source

pub fn try_as_number(&self) -> Option<Number>

If this is a Number expression, return that. Otherwise return None.

Source

pub fn try_normal(&self) -> Option<&Normal>

👎Deprecated: Use Expr::try_as_normal() instead
Source

pub fn try_symbol(&self) -> Option<&Symbol>

👎Deprecated: Use Expr::try_as_symbol() instead
Source

pub fn try_number(&self) -> Option<Number>

👎Deprecated: Use Expr::try_as_number() instead
Source§

impl Expr

Source

pub fn new(kind: ExprKind) -> Expr

Construct a new expression from an ExprKind.

Source

pub fn to_kind(self) -> ExprKind

Consume self and return an owned ExprKind.

If the reference count of self is equal to 1 this function will not perform a clone of the stored ExprKind, making this operation very cheap in that case.

Source

pub fn kind(&self) -> &ExprKind

Get the ExprKind representing this expression.

Source

pub fn kind_mut(&mut self) -> &mut ExprKind

Get mutable access to the ExprKind that represents this expression.

If the reference count of the underlying shared pointer is not equal to 1, this will clone the ExprKind to make it unique.

Source

pub fn ref_count(&self) -> usize

Retrieve the reference count of this expression.

Source

pub fn normal<H: Into<Expr>>(head: H, contents: Vec<Expr>) -> Expr

Construct a new normal expression from the head and elements.

Source

pub fn symbol<S: Into<Symbol>>(s: S) -> Expr

Construct a new expression from a Symbol.

Source

pub fn number(num: Number) -> Expr

Construct a new expression from a Number.

Source

pub fn string<S: Into<String>>(s: S) -> Expr

Construct a new expression from a String.

Source

pub fn real(real: f64) -> Expr

Construct an expression from a floating-point number.

let expr = Expr::real(3.14159);
§Panics

This function will panic if real is NaN.

Source

pub fn tag(&self) -> Option<Symbol>

Returns the outer-most symbol “tag” used in this expression.

To illustrate:

ExpressionTag
5None
"hello"None
foofoo
f[1, 2, 3]f
g[x][y]g
Source

pub fn normal_head(&self) -> Option<Expr>

If this represents a Normal expression, return its head. Otherwise, return None.

Source

pub fn normal_part(&self, index_0: usize) -> Option<&Expr>

Attempt to get the element at index of a Normal expression.

Return None if this is not a Normal expression, or the given index is out of bounds.

index is 0-based. The 0th index is the first element, not the head.

This function does not panic.

Source

pub fn has_normal_head(&self, sym: &Symbol) -> bool

Returns true if self is a Normal expr with the head sym.

Source

pub fn null() -> Expr

Null WL.

Source

pub fn rule<LHS: Into<Expr>>(lhs: LHS, rhs: Expr) -> Expr

Construct a new Rule[_, _] expression from the left-hand side and right-hand side.

§Example

Construct the expression FontSize -> 16:

use wolfram_expr::{Expr, Symbol};

let option = Expr::rule(Symbol::new("System`FontSize"), Expr::from(16));
Source

pub fn rule_delayed<LHS: Into<Expr>>(lhs: LHS, rhs: Expr) -> Expr

Construct a new RuleDelayed[_, _] expression from the left-hand side and right-hand side.

§Example

Construct the expression x :> RandomReal[]:

use wolfram_expr::{Expr, Symbol};

let delayed = Expr::rule_delayed(
    Symbol::new("Global`x"),
    Expr::normal(Symbol::new("System`RandomReal"), vec![])
);
Source

pub fn list(elements: Vec<Expr>) -> Expr

Construct a new List[...]({...}) expression from it’s elements.

§Example

Construct the expression {1, 2, 3}:

use wolfram_expr::Expr;

let list = Expr::list(vec![Expr::from(1), Expr::from(2), Expr::from(3)]);

Trait Implementations§

Source§

impl Clone for Expr

Source§

fn clone(&self) -> Expr

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Expr

Source§

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

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

impl Display for Expr

By default, this should generate a string which can be unambiguously parsed to reconstruct the Expr being displayed. This means symbols will always include their contexts, special characters in String’s will always be properly escaped, and numeric literals needing precision and accuracy marks will have them.

Source§

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

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

impl From<&String> for Expr

Source§

fn from(s: &String) -> Expr

Converts to this type from the input type.
Source§

impl From<&Symbol> for Expr

Source§

fn from(sym: &Symbol) -> Expr

Converts to this type from the input type.
Source§

impl From<&str> for Expr

Source§

fn from(s: &str) -> Expr

Converts to this type from the input type.
Source§

impl From<Normal> for Expr

Source§

fn from(normal: Normal) -> Expr

Converts to this type from the input type.
Source§

impl From<String> for Expr

Source§

fn from(s: String) -> Expr

Converts to this type from the input type.
Source§

impl From<Symbol> for Expr

Source§

fn from(sym: Symbol) -> Expr

Converts to this type from the input type.
Source§

impl From<bool> for Expr

Source§

fn from(value: bool) -> Expr

Converts to this type from the input type.
Source§

impl From<i16> for Expr

Source§

fn from(int: i16) -> Expr

Converts to this type from the input type.
Source§

impl From<i32> for Expr

Source§

fn from(int: i32) -> Expr

Converts to this type from the input type.
Source§

impl From<i64> for Expr

Source§

fn from(int: i64) -> Expr

Converts to this type from the input type.
Source§

impl From<i8> for Expr

Source§

fn from(int: i8) -> Expr

Converts to this type from the input type.
Source§

impl From<u16> for Expr

Source§

fn from(int: u16) -> Expr

Converts to this type from the input type.
Source§

impl From<u32> for Expr

Source§

fn from(int: u32) -> Expr

Converts to this type from the input type.
Source§

impl From<u8> for Expr

Source§

fn from(int: u8) -> Expr

Converts to this type from the input type.
Source§

impl Hash for Expr

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq<Symbol> for Expr

Source§

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

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

const 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 PartialEq for Expr

Source§

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

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

const 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 Eq for Expr

Source§

impl StructuralPartialEq for Expr

Auto Trait Implementations§

§

impl Freeze for Expr

§

impl RefUnwindSafe for Expr

§

impl Send for Expr

§

impl Sync for Expr

§

impl Unpin for Expr

§

impl UnwindSafe for Expr

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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, 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.