Struct rusymbols::Expression[][src]

pub struct Expression { /* fields omitted */ }

Expression struct

Probably the most important structure of this crate. It provides a tree-like structure for expressions. The basic idea is that the Expression contains two arguments and the action to be done with them. But so far, there are situations when there cannot be exactly two arguments (for example, an ordinary number or a variable), so Actions have taken responsibility for such cases.

Example

use rusymbols::Expression;
let x = Expression::new_var("x"); // new 'x' variable creation
let y = Expression::new_var("y"); // new 'y' variable creation
let expr = x + y; // expression

assert_eq!(expr.to_string(), "x + y")

Beware of automatic parentheses

use rusymbols::Expression;
let x = Expression::new_var("x"); // new 'x' variable creation
let y = Expression::new_var("y"); // new 'y' variable creation
let z = Expression::new_var("z"); // new 'z' variable creation
// according to the rules of arithmetic, multiplication takes precedence over addition
let expr = x.clone() + y.clone() * z.clone(); // equivalent to x + (y * z)

let mut expr_2 = x.clone() + y.clone(); // equivalent to x + y
expr_2 = expr_2 * z.clone(); // equivalent to (x + y) * z

let expr_3 = (x + y) * z;

assert_ne!(expr.to_string(), expr_2.to_string());
assert_eq!(expr_2.to_string(), expr_3.to_string());

assert_eq!(expr.to_string(), "x + y * z");
assert_eq!(expr_2.to_string(), "(x + y) * z");

Implementations

impl Expression[src]

Associated functions

pub fn new(left: Expression, right: Expression, kind: Actions) -> Expression[src]

New Expression constructor

Example

use rusymbols::{core, Expression};
let x = Expression::new_var("x"); // new 'x' variable creation
let two = Expression::new_val(2.0); // new 2.0 value creation
let expr = Expression::new(x, two, core::Actions::Mul); // new expression creation

assert_eq!(expr.to_string(), "x * 2")

pub fn new_var(literal: &str) -> Expression[src]

New Expression constructor with variable literal

Example

use rusymbols::{core, Expression};
let x = Expression::new_var("x"); // new 'x' variable creation
let y = Expression::new_var("y"); // new 'y' value creation
let expr = x / y;

assert_eq!(expr.to_string(), "x / y")

pub fn new_val(value: f64) -> Expression[src]

New Expression constructor with numeric value

Example

use rusymbols::{core, Expression};
use std::collections::HashMap;

let ten = Expression::new_val(10.0); // new 'x' variable creation
let two = Expression::new_val(2.0); // new 2.0 value creation
let expr = ten * two; // new expression creation
let args: HashMap<&str, f64> = HashMap::new();

assert_eq!(expr.to_string(), "10 * 2");
assert_eq!(expr.eval_args(&args).unwrap(), 20.0);

pub fn new_brackets(expression: Expression, brackets: Brackets) -> Expression[src]

New Expression in brackets

impl Expression[src]

Taking ownership of data

pub fn brackets(self, brackets: Brackets) -> Expression[src]

Wraps the given Expression with the specified parentheses

Example

use rusymbols::Expression;
use rusymbols::core::Brackets;
let x = Expression::new_var("x");
let expr = x.brackets(Brackets::Square);

assert_eq!(expr.to_string(), "[x]")

pub fn brackets_round(self) -> Expression[src]

Wraps the given Expression with the round brackets

Example

use rusymbols::Expression;
let x = Expression::new_var("x");
let expr = x.brackets_round();

assert_eq!(expr.to_string(), "(x)")

pub fn pow(self, rhs: Self) -> Self[src]

Raises an Expression to a specified power

Example

use rusymbols::Expression;
let x = Expression::new_var("x");
let y = Expression::new_var("y");
let expr = x.pow(y);

assert_eq!(expr.to_string(), "x**y")

impl Expression[src]

Borrowing methods

pub fn eval_args(&self, args: &HashMap<&str, f64>) -> Option<f64>[src]

Tries to get the numeric value of an Expression by replacing variables with values from a HashMap

Returns None if the expression cannot be evaluated (e.g. insufficient variable values were passed)

Usage

const LITERAL_X: &str = "x";
const LITERAL_Y: &str = "y";

use std::collections::HashMap;
use rusymbols::Expression;

let mut args: HashMap<&str, f64> = HashMap::new();
args.insert(LITERAL_X, 2.0);
args.insert(LITERAL_Y, 2.0);

let x = Expression::new_var(LITERAL_X);
let y = Expression::new_var(LITERAL_Y);
let expr = x * y;

assert_eq!(expr.eval_args(&args).unwrap(), 4.0);

Trait Implementations

impl Add<Expression> for Expression[src]

type Output = Expression

The resulting type after applying the + operator.

impl Clone for Expression[src]

impl Debug for Expression[src]

impl Default for Expression[src]

impl Display for Expression[src]

impl Div<Expression> for Expression[src]

type Output = Self

The resulting type after applying the / operator.

impl Mul<Expression> for Expression[src]

type Output = Expression

The resulting type after applying the * operator.

impl Neg for Expression[src]

type Output = Expression

The resulting type after applying the - operator.

impl Sub<Expression> for Expression[src]

type Output = Expression

The resulting type after applying the - operator.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.