标签: println

为什么不打印!在Rust单元测试中工作?

我已经实现了以下方法和单元测试:

use std::fs::File;
use std::path::Path;
use std::io::prelude::*;

fn read_file(path: &Path) {
    let mut file = File::open(path).unwrap();
    let mut contents = String::new();
    file.read_to_string(&mut contents).unwrap();
    println!("{}", contents);
}

#[test]
fn test_read_file() {
    let path = &Path::new("/etc/hosts");
    println!("{:?}", path);
    read_file(path);
}
Run Code Online (Sandbox Code Playgroud)

我以这种方式运行单元测试:

rustc --test app.rs; ./app
Run Code Online (Sandbox Code Playgroud)

我也可以运行它

cargo test
Run Code Online (Sandbox Code Playgroud)

我收到一条消息说测试已通过,但println!屏幕上从未显示过.为什么不?

println rust

237
推荐指数
7
解决办法
4万
查看次数

go中fmt.Println()和println()之间的区别是什么

在go中,如果我们想要打印一些东西,我们可以这样做:

import "fmt"

func main(){
    fmt.Println("Hello world!")
}
Run Code Online (Sandbox Code Playgroud)

但是我发现没有导入就可以做同样的事情fmt:

func main(){
    println("Hello world!")
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下吗?

go println

109
推荐指数
3
解决办法
3万
查看次数

Java中的整数除法

这是一个基本问题,但我找不到答案.我已经研究了浮点运算和其他一些主题,但似乎没有任何东西可以解决这个问题.我确定我的术语错了.

基本上,我想要两个数量 - 完成和总数 - 并将它们分成一个百分比(已经完成了多少).数量是longs.这是设置:

long completed = 25000;
long total = 50000;

System.out.println(completed/total);  // Prints 0
Run Code Online (Sandbox Code Playgroud)

我已经尝试将结果重新分配给双 - 它打印0.0.我哪里错了?

顺便提一下,下一步是将这个结果乘以100,一旦这个小障碍被推翻,我认为应该很容易.

顺便说一句,这里没有作业,只是普通的老字号(今天可能编码过多).

java math floating-point println long-integer

87
推荐指数
3
解决办法
17万
查看次数

Kotlin没有看到Java Lombok访问器?

使用Kotlin 1.0.0版本(在IntelliJ 15中编译).

println(myPojoInstance.foo)
Run Code Online (Sandbox Code Playgroud)

当它尝试编译引用基于Lombok的POJO的代码(在IntelliJ或Gradle中)时,它会给出错误"无法访问'foo':它在'MyPojo'中是'私有'.这是真的,它们都是私有的,我的对象有@Value @Builder用于lombok注释.

我试过专门调用getFoo(),但它说"getFoo的未解析引用".也许有一些技巧可以让Kotlin意识到如何处理lombok注释?

println lombok kotlin

57
推荐指数
4
解决办法
1万
查看次数

如何打印Vec?

我尝试了以下代码:

fn main() {
    let v2 = vec![1; 10];
    println!("{}", v2);
}
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨说:

error[E0277]: `std::vec::Vec<{integer}>` doesn't implement `std::fmt::Display`
 --> src/main.rs:3:20
  |
3 |     println!("{}", v2);
  |                    ^^ `std::vec::Vec<{integer}>` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `std::vec::Vec<{integer}>`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: required by `std::fmt::Display::fmt`
Run Code Online (Sandbox Code Playgroud)

有没有人实现这个特性Vec<T>

println rust

50
推荐指数
6
解决办法
2万
查看次数

46
推荐指数
2
解决办法
10万
查看次数

print中的println vs System.out.println

我一直认为这Predef.println只是一个捷径System.out.println,但显然我错了,因为它似乎根本没用System.out.为什么会这样?我怎样才能System.out在Scala中进行下面的"重定向" ?

scala> val baos = new java.io.ByteArrayOutputStream
baos: java.io.ByteArrayOutputStream = 

scala> val ps = new java.io.PrintStream(baos)
ps: java.io.PrintStream = java.io.PrintStream@6c5ac4

scala> System.setOut(ps)

scala> println("hello")
hello

scala> new String(baos.toByteArray)
res2: java.lang.String = ""

scala> System.out.println("hello")

scala> new String(baos.toByteArray)
res7: java.lang.String = 
"hello
"
Run Code Online (Sandbox Code Playgroud)

compatibility scala println

45
推荐指数
1
解决办法
4万
查看次数

在新行中打印,java

我有以下代码:

    System.out.println(" | 1  2  3  4  5  6  7  8  9");
    System.out.println("----------------------------");
    System.out.println("");
Run Code Online (Sandbox Code Playgroud)

我使用println创建一个新行.是否可以使用\n或\ r来做同样的事情?我试图将\n添加到第二个println语句并继续使用print方法打印但是\n不会创建新行.

有任何想法吗?

java printing new-operator println

40
推荐指数
7
解决办法
41万
查看次数

在Swift中执行println字典项时,转义字典键双引号

我一直在玩Swift,刚遇到一个问题.我有以下字典:

var locations:Dictionary<String,CLLocationCoordinate2D> = ["current":CLLocationCoordinate2D(latitude: lat, longitude: lng) ];

println("current locaition is \(locations["current"])")
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨双引号current代表我字典中的一个键.

我试图逃避它,\但这不是正确的方法.

感谢任何帮助.

println swift

35
推荐指数
2
解决办法
9640
查看次数

print中的scala for-comprehension

为了理解,我不能只是提出一个印刷声明:

def prod (m: Int) = {
  for (a <- 2 to m/(2*3);
    print (a + "  ");
    b <- (a+1) to m/a;
    c = (a*b) 
    if (c < m)) yield c
}
Run Code Online (Sandbox Code Playgroud)

但我可以通过虚拟任务轻松绕过它:

def prod (m: Int) = {
  for (a <- 2 to m/(2*3);
    dummy = print (a + "  ");
    b <- (a+1) to m/a;
    c = (a*b) 
    if (c < m)) yield c
}
Run Code Online (Sandbox Code Playgroud)

作为副作用,并且仅在开发中的代码中使用(到目前为止),是否有更好的临时解决方案?

是否有一个严重的问题为什么我不应该使用它,除了副作用?

更新显示真实代码,其中调整一个解决方案比预期更难:

从与Rex Kerr的讨论来看,必要性已经上升到显示原始代码,这有点复杂,但似乎与问题无关(2x .filter,最后调用方法),但是当我试图将Rex'模式应用到它我失败了,所以我在这里发布:

  def prod (p: Array[Boolean], max: Int) …
Run Code Online (Sandbox Code Playgroud)

scala variable-assignment println for-comprehension

29
推荐指数
2
解决办法
1万
查看次数