Struct wolfram_expr::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
impl Expr
sourcepub fn try_as_normal(&self) -> Option<&Normal>
pub fn try_as_normal(&self) -> Option<&Normal>
If this is a Normal
expression, return that. Otherwise return None.
sourcepub fn try_as_bool(&self) -> Option<bool>
pub fn try_as_bool(&self) -> Option<bool>
sourcepub fn try_as_str(&self) -> Option<&str>
pub fn try_as_str(&self) -> Option<&str>
If this is a ExprKind::String
expression, return that. Otherwise return None.
sourcepub fn try_as_symbol(&self) -> Option<&Symbol>
pub fn try_as_symbol(&self) -> Option<&Symbol>
If this is a Symbol
expression, return that. Otherwise return None.
sourcepub fn try_as_number(&self) -> Option<Number>
pub fn try_as_number(&self) -> Option<Number>
If this is a Number
expression, return that. Otherwise return None.
pub fn try_normal(&self) -> Option<&Normal>
pub fn try_symbol(&self) -> Option<&Symbol>
pub fn try_number(&self) -> Option<Number>
source§impl Expr
impl Expr
sourcepub fn to_kind(self) -> ExprKind
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.
sourcepub fn normal<H: Into<Expr>>(head: H, contents: Vec<Expr>) -> Expr
pub fn normal<H: Into<Expr>>(head: H, contents: Vec<Expr>) -> Expr
Construct a new normal expression from the head and elements.
sourcepub fn real(real: f64) -> Expr
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.
sourcepub fn tag(&self) -> Option<Symbol>
pub fn tag(&self) -> Option<Symbol>
Returns the outer-most symbol “tag” used in this expression.
To illustrate:
Expression | Tag |
---|---|
5 | None |
"hello" | None |
foo | foo |
f[1, 2, 3] | f |
g[x][y] | g |
sourcepub fn normal_head(&self) -> Option<Expr>
pub fn normal_head(&self) -> Option<Expr>
If this represents a Normal
expression, return its head. Otherwise, return
None
.
sourcepub fn normal_part(&self, index_0: usize) -> Option<&Expr>
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.
sourcepub fn has_normal_head(&self, sym: &Symbol) -> bool
pub fn has_normal_head(&self, sym: &Symbol) -> bool
Returns true
if self
is a Normal
expr with the head sym
.
sourcepub fn rule<LHS: Into<Expr>>(lhs: LHS, rhs: Expr) -> Expr
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));
sourcepub fn rule_delayed<LHS: Into<Expr>>(lhs: LHS, rhs: Expr) -> Expr
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![])
);
Trait Implementations§
source§impl Display for Expr
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.