这是从文件中读取行并将它们分解成Rust中的单词的正确方法吗?(Is this the right way to read lines from file and split them into words in Rust?)
编者按:本代码示例来自1.0之前的Rust版本,并且不具有语法上有效的Rust 1.0代码。 此代码的更新版本会产生不同的错误,但答案仍然包含有价值的信息。
我已经实现了下面的方法来从2维数据结构中的文件中返回单词:
fn read_terms() -> Vec<Vec<String>> { let path = Path::new("terms.txt"); let mut file = BufferedReader::new(File::open(&path)); return file.lines().map(|x| x.unwrap().as_slice().words().map(|x| x.to_string()).collect()).collect(); }
这是否是Rust中正确,习惯和有效的方式? 我想知道
collect()
需要经常调用,以及是否需要在这里调用to_string()
来分配内存。 也许返回类型应该被定义为不同的地方,以更加地道和有效?Editor's note: This code example is from a version of Rust prior to 1.0 and is not syntactically valid Rust 1.0 code. Updated versions of this code produce different errors, but the answers still contain valuable information.
I've implemented the following method to return me the words from a file in a 2 dimensional data structure:
fn read_terms() -> Vec<Vec<String>> { let path = Path::new("terms.txt"); let mut file = BufferedReader::new(File::open(&path)); return file.lines().map(|x| x.unwrap().as_slice().words().map(|x| x.to_string()).collect()).collect(); }
Is this the right, idiomatic and efficient way in Rust? I'm wondering if
collect()
needs to be called so often and whether it's necessary to callto_string()
here to allocate memory. Maybe the return type should be defined differently to be more idiomatic and efficient?
原文:https://stackoverflow.com/questions/25581463
满意答案
找出问题所在,我将分享我的见解:
addJavascriptInterface函数仅在调用BEFORE loadUrl / loadData函数时才适用。
在我的情况下 - 我期望addJavascriptInterface注入一个JS桥,但我从来没有重新加载WebView内容,因此从未主动注入。
重新加载WebView HTML内容后,桥接器按预期添加。
Figured out the problem, so I'll share my insights:
The addJavascriptInterface function applies only if called BEFORE a loadUrl / loadData function.
In my case - I expected addJavascriptInterface to inject a JS bridge, but I never reloaded the WebView content, so it was never actively injected.
After reloading the WebView HTML content, the bridge was added as expected.
相关问答
更多可抽取的api <21(Drawable tinting for api <21)
addJavascriptInterface函数在API 21上不起作用(addJavascriptInterface function doesn't work on API 21)
LibGDX无法在Android API 21上运行?(LibGDX not work on Android API 21?)
比较API 21 x API 23中的资源(Comparing resources in API 21 x API 23)
Snackbar API 21(Snackbar with API 21)
API 21中的CollapsingToolbarLayout(CollapsingToolbarLayout in API 21)
API 21下的主要暗色android(primary dark color android under API 21)
CUDA线程块大小1024不起作用(cc = 20,sm = 21)(CUDA thread block size 1024 doesn't work (cc=20, sm=21))
为什么〜 - (2 +“2”)是21?(Why ~-(2 + “2”) is 21?)
Youtube Api playVideo方法在某些移动设备上不起作用(Youtube Api playVideo method doesn't work on some mobile devices)
相关文章
更多Java 流(Stream)、文件(File)和IO
Hadoop 自定义InputFormat实现自定义Split
java String.split(“.”)和String.split(“|”)的注意事项
【HDFS】HADOOP DISTRIBUTED FILE SYSTEM
Failed to read auto-increment value from storage engine错误的解决方法
用‘button’跟‘text’组合代替‘file’,选择文件后点‘submit’,‘file’的值被清空
PHP 中dirname(_file_)
xxx is not in the sudoers file解决方法
file_get_contents 无法读取https的问题解决!
shell 脚本执行,出现错误bad interpreter: No such file or directory
最新问答
更多python的访问器方法有哪些
使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
透明度错误IE11(Transparency bug IE11)
linux的基本操作命令。。。
响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
元素上的盒子阴影行为(box-shadow behaviour on elements)
它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂
Laravel检查是否存在记录(Laravel Checking If a Record Exists)
这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found
设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640
Copyright ©2023
peixunduo.com All
Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!
Laravel检查是否存在记录(Laravel Checking If a Record Exists)
设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!