首页 > 其他分享 >[Rust] Specifying a function argument can be mutated

[Rust] Specifying a function argument can be mutated

时间:2024-02-27 15:44:07浏览次数:17  
标签:function mut Vec move mutated Specifying vec0 fill vec

Following code has compile error:

#[test]
fn main() {
    let vec0 = vec![22, 44, 66];

    let mut vec1 = fill_vec(vec0);

    assert_eq!(vec1, vec![22, 44, 66, 88]);
}

fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
    vec.push(88);

    vec
}
Progress: [##########################>---------------------------------] 42/96 (43.8 %)                             
⚠️  Compiling of exercises/move_semantics/move_semantics3.rs failed! Please try again. Here's the output:
warning: variable does not need to be mutable
  --> exercises/move_semantics/move_semantics3.rs:14:9
   |
14 |     let mut vec1 = fill_vec(vec0);
   |         ----^^^^
   |         |
   |         help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

error[E0596]: cannot borrow `vec` as mutable, as it is not declared as mutable
  --> exercises/move_semantics/move_semantics3.rs:20:5
   |
20 |     vec.push(88);
   |     ^^^ cannot borrow as mutable
   |
help: consider changing this to be mutable
   |
19 | fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
   |             +++

error: aborting due to previous error; 1 warning emitted

 

Follow the suggest, mark argument as mut:

#[test]
fn main() {
    let vec0 = vec![22, 44, 66];

    let mut vec1 = fill_vec(vec0);

    assert_eq!(vec1, vec![22, 44, 66, 88]);
}

fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
    vec.push(88);

    vec
}

 

标签:function,mut,Vec,move,mutated,Specifying,vec0,fill,vec
From: https://www.cnblogs.com/Answer1215/p/18036996

相关文章

  • ../inst/include/Eigen/src/Core/MathFunctions.h:487:16: error: no member named 'R
    Asmentionedin conda-forge/r-base-feedstock#163(comment),IsuccessfullyinstalledsctransforminMacsiliconM1Maxbyfirstrun exportPKG_CPPFLAGS="-DHAVE_WORKING_LOG1P intheterminalandtheninstallthepackageinR.......
  • Semantic Kernel 学习笔记:体验基于 prompt function 实现的 Plugin
    在一个SemanticKernelplugin中可以创建两种类型的function,分别是nativefunction与promptfunction(之前叫semanticfunction)。下面这款plugin中给C#method添加了[KernelFunction]attribute,就是nativefunctionpublicclassLightPlugin{publicboolIsOn......
  • [Rust] Implicitly returning values from functions
    Codehaserror:fnmain(){letanswer=square(3);println!("Thesquareof3is{}",answer);}fnsquare(num:i32)->i32{num*num;}Error:⚠️Compilingofexercises/functions/functions5.rsfailed!Pleasetryagain.Here&#......
  • let、const、var、function所谓的”变量提升“、暂时性死区到底是什么
    今天看了大佬一个文章我用了两个月的时间才理解let-知乎(zhihu.com),文章中其实说得很清楚,还有大佬解决这个问题的整个心路历程。我这里做一个总结记录,专注于“变量提升”、暂时性死区这两个点做一个讨论。现象讨论下面这两段代码,我们都知道这段代码在控制台会打印undefined......
  • Machine Learning - The Sigmoid Function
    CalculateNodeOutput.TaskYouaregiventhevaluesforw1,w2,b,x1andx2andyoumustcomputetheoutputforthenode.Usethesigmoidastheactivationfunction.InputFormatw1,w2,b,x1andx2ononelineseparatedbyspacesOutputFormatFloatrounded......
  • 【Azure Function】示例运行 python durable function(model V2)
    问题描述参考官方文档(使用Python创建你的第一个持久函数:https://learn.microsoft.com/zh-cn/azure/azure-functions/durable/quickstart-python-vscode),部署后,却出现“Failedtoloadfunction”错误。在结合以上参考文档后,可以通过如下的步骤创建并运行PythonDurableFu......
  • 一文搞懂Flink Window机制 Windows和 Function 和 Process组合处理事件
    一文搞懂FlinkWindow机制和Function和Process组合处理事件Windows是处理无线数据流的核心,它将流分割成有限大小的桶(buckets),并在其上执行各种计算。Windows是处理无线数据流的核心,它将流分割成有限大小的桶(buckets),并在其上执行各种计算。窗口化的Flink程......
  • Flink 增量窗口聚合函数 ReduceFunction(归约函数)和AggregateFunction(聚合函数)
    Flink增量窗口聚合函数定义了窗口分配器,只是知道了数据属于哪个窗口,可以将数据收集起来了;至于收集起来到底要做什么,其实还完全没有头绪。所以在窗口分配器之后,必须再接上一个定义窗口如何进行计算的操作,这就是所谓的“窗口函数”(windowfunctions)。经窗口分配器处理之后,数据可......
  • 绕过disable_functions的限制
    https://github.com/AntSwordProject/AntSword-Labs/tree/master/bypass_disable_functionshttps://wiki.luoyunhao.com/web/Bypassdisable_function绕过disable_functions的限制disable_functions是php.ini中的一个设置选项,可以用来设置PHP环境禁止使用某些函数,通常是网站......
  • 【Azure Function App】在VS Code中,创建好Function App后部署到Azure中,无法选择Subscr
    问题描述在VSCode中,创建好FunctionApp后部署到Azure中,无法选择Subscriptions问题解答对于无法使用VSCode 部署FunctionApp 到Azure,最近有一个更新,导致了AzureResource 插件的 v0.8.0 版本不支持中国区登录目前的解决办法是:通过手动安装的方式把VSCode中的Azu......