如何使用另一个箱子中定义的宏?

时间:2016-10-09 16:32:06

标签: rust

我已经看过几个使用:包创建Python模块的教程,但在构建时仍然有错误:

:

我明白了:

cpython

我从哪里得到它?我正在使用Rust 1.12。

UPD

  1. 需要添加extern crate cpython; use cpython::{PyObject, PyResult, Python, PyTuple, PyDict, ToPyObject, PythonObject}; fn add_two(py: Python, args: &PyTuple, _: Option<&PyDict>) -> PyResult<PyObject> { match args.as_slice() { [ref a_obj, ref b_obj] => { let a = a_obj.extract::<i32>(py).unwrap(); let b = b_obj.extract::<i32>(py).unwrap(); let mut acc:i32 = 0; for _ in 0..1000 { acc += a + b; } Ok(acc.to_py_object(py).into_object()) }, _ => Ok(py.None()) } } py_module_initializer!(example, |py, module| { try!(module.add(py, "add_two", py_fn!(add_two))); Ok(()) }); (如答案中所示)
  2. 有关其他错误,请参阅examples here

2 个答案:

答案 0 :(得分:4)

您可能需要声明cpython,如下所示:

#[macro_use] extern crate cpython;

能够使用cpython的宏。您可以咨询the example in its docs

答案 1 :(得分:2)

您必须在#[macro_use] extern crate cpython; 声明中添加Enabling Cross-Origin Requests in ASP.NET Web API 2属性,要求编译器将crate导出的宏带入crate的命名空间。

import java.util.Arrays;
public class ReverseTheWords {

    public static void main(String[] args) {
        String s = "hello java how do you do";
        System.out.println(Arrays.toString(ReverseTheWords.split(s)));
    }

    public static String[] split(String s) {
        int count = 0;
        char[] c = s.toCharArray();

        for (int i = 0; i < c.length; i++) {
            if (c[i] == ' ') {
                count++;
            }
        }
        String temp = "";
        int k = 0;
        String[] rev = new String[count + 1];
        for (int i = 0; i < c.length; i++) {
            if (c[i] == ' ') {
                rev[k++] = temp;
                temp = "";
            } else
                temp = temp + c[i];
        }
        rev[k] = temp;
        return rev;
    }

}