pub struct Symbol(/* private fields */);Expand description
Wolfram Language symbol.
§PartialOrd sorting order
The comparison behavior of this type is NOT guaranteed to match the behavior of
System`Order for symbols (and does not match it at the moment).
This type implements PartialOrd/Ord primarily for the purposes of allowing
instances of this type to be included in ordered sets (e.g. BTreeMap).
Implementations§
Source§impl Symbol
impl Symbol
Sourcepub fn try_new(input: &str) -> Option<Symbol>
pub fn try_new(input: &str) -> Option<Symbol>
Attempt to parse input as an absolute symbol.
An absolute symbol is a symbol with an explicit context path. "System`Plus" is
an absolute symbol, "Plus" is a relative symbol and/or a SymbolName.
"`Plus" is also a relative symbol.
Sourcepub fn new(input: &str) -> Symbol
pub fn new(input: &str) -> Symbol
Construct a symbol from input.
§Panics
This function will panic if input is not a valid Wolfram Language symbol.
Symbol::try_new(input) must succeed.
This method is intended to be used for convenient construction of symbols from string literals, where an error is unlikely to occur, e.g.:
let expr = Expr::normal(Symbol::new("MyPackage`Foo"), vec![]);If not using a string literal as the argument, prefer to use Symbol::try_new
and handle the error condition.
Examples found in repository?
More examples
9fn test_runtime_function_from_main_thread() -> bool {
10 let expr = Expr::normal(Symbol::new("System`Plus"), vec![
11 Expr::from(2),
12 Expr::from(2),
13 ]);
14
15 wll::evaluate(&expr) == Expr::from(4)
16}
17
18#[wll::export]
19fn test_runtime_function_from_non_main_thread() -> String {
20 let child = std::thread::spawn(|| {
21 panic::set_hook(Box::new(|_| {
22 // Do nothing, just to avoid printing panic message to stderr.
23 }));
24
25 let result = panic::catch_unwind(|| {
26 wll::evaluate(&Expr::normal(Symbol::new("System`Plus"), vec![
27 Expr::from(2),
28 Expr::from(2),
29 ]))
30 });
31
32 // Restore the previous (default) hook.
33 let _ = panic::take_hook();
34
35 result
36 });
37
38 let result = child.join().unwrap();
39
40 match result {
41 Ok(_) => "didn't panic".to_owned(),
42 // We expect the thread to panic
43 Err(panic) => {
44 if let Some(str) = panic.downcast_ref::<&str>() {
45 format!("PANIC: {}", str)
46 } else if let Some(string) = panic.downcast_ref::<String>() {
47 format!("PANIC: {}", string)
48 } else {
49 "PANIC".to_owned()
50 }
51 },
52 }
53}176fn expr_string_join(link: &mut Link) {
177 let expr = link.get_expr().unwrap();
178
179 let list = expr.try_as_normal().unwrap();
180 assert!(list.has_head(&Symbol::new("System`List")));
181
182 let mut buffer = String::new();
183 for elem in list.elements() {
184 match elem.kind() {
185 ExprKind::String(str) => buffer.push_str(str),
186 _ => panic!("expected String argument, got: {:?}", elem),
187 }
188 }
189
190 link.put_str(buffer.as_str()).unwrap()
191}7fn generate_message(_: Vec<Expr>) {
8 // Construct the expression `Message[MySymbol::msg, "..."]`.
9 let message = Expr::normal(Symbol::new("System`Message"), vec![
10 // MySymbol::msg is MessageName[MySymbol, "msg"]
11 Expr::normal(Symbol::new("System`MessageName"), vec![
12 Expr::from(Symbol::new("Global`MySymbol")),
13 Expr::string("msg"),
14 ]),
15 Expr::string("a Rust LibraryLink function"),
16 ]);
17
18 // Evaluate the message expression.
19 let _: Expr = wll::evaluate(&message);
20}70fn get_instance_data(args: Vec<Expr>) -> Expr {
71 assert!(args.len() == 1, "get_instance_data: expected 1 argument");
72
73 let id: u32 = unwrap_id_arg(&args[0]);
74
75 let MyObject { value } = {
76 let instances = INSTANCES.lock().unwrap();
77
78 instances
79 .get(&id)
80 .cloned()
81 .expect("instance does not exist")
82 };
83
84 Expr::normal(Symbol::new("System`Association"), vec![Expr::normal(
85 Symbol::new("System`Rule"),
86 vec![Expr::string("Value"), Expr::string(value)],
87 )])
88}Sourcepub fn as_symbol_ref(&self) -> SymbolRef<'_>
pub fn as_symbol_ref(&self) -> SymbolRef<'_>
Sourcepub fn context(&self) -> ContextRef<'_>
pub fn context(&self) -> ContextRef<'_>
Get the context path part of a symbol as an ContextRef.
Sourcepub fn symbol_name(&self) -> SymbolNameRef<'_>
pub fn symbol_name(&self) -> SymbolNameRef<'_>
Get the symbol name part of a symbol as a SymbolNameRef.