为什么在 jupyter notebook 中使用 #[derive(Debug)] 生锈时没有打印输出?

why there is no printout when using #[derive(Debug)] for rust in jupyter notebook?

我是生锈学习新手。当我试图在 jupyter-notebook 中执行一些由 Rust 编写的代码时。我无法打印出来。但它可以通过 vscode 或终端执行。 no print out in jupyter-notebook

这是我的代码:

// Derive the `fmt::Debug` implementation for `Structure`. `Structure`
// is a structure which contains a single `i32`.
#[derive(Debug)]
struct Structure(i32);

// Put a `Structure` inside of the structure `Deep`. Make it printable
// also.
#[derive(Debug)]
struct Deep(Structure);

fn main() {
    // Printing with `{:?}` is similar to with `{}`.
    println!("{:?} months in a year.", 12);
    println!("{1:?} {0:?} is the {actor:?} name.",
             "Slater",
             "Christian",
             actor="actor's");

    // `Structure` is printable!
    println!("Now {:?} will print!", Structure(3));
    
    // The problem with `derive` is there is no control over how
    // the results look. What if I want this to just show a `7`?
    println!("Now {:?} will print!", Deep(Structure(7)));
}

在 vscode 中成功 运行 的快照 successful print out

那么,怎么了? evcxr 有任何错误吗?

这里的问题是 fn main 的用法。参见 https://github.com/google/evcxr/issues/183

在 EVCXR 笔记本中,main 不是特殊功能。因此,编写以下内容只是创建一个名为 main:

的函数
fn main() {
    println!("Hello, world!");
}

要使 main 中的代码实际上 运行,需要显式调用 main

main()
// => Hello, world!

通常,在 jupyter notebook 中根本不会使用 main 函数。这当然不同于需要 main 函数的普通 rust 可执行文件。


找到问题根本原因的功劳归功于@Jun Lv。我只是添加了一些关于修复 works/problem 发生的原因的推理。